Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to Send Multiple writeUTF to Socket in TCP?

Explorer ,
Apr 20, 2015 Apr 20, 2015

So with TCP in AS3, I'm trying to write strings over to the server and then to the clients, but it appears to only be able to read one at a time. This is not good because I'm sending messages based on keys being pressed, and the client has an action that needs to be taken place based on what key is pressed. Since multiple keys can be pressed at a time, obviously it does not work correctly.

Client example:

if (keys[p1_jump_key])

        {

            p1_up = "down";

            sock.writeUTF("p1_up_down");

            sock.flush();

        }

        else

        {

            p1_up = "up";

            sock.writeUTF("p1_up_up");

            sock.flush();

        }

        if (keys[p1_crouch_key])

        {

            p1_down = "down";

            sock.writeUTF("p1_down_down");

            sock.flush();

        }

        else

        {

            p1_down = "up";

            sock.writeUTF("p1_down_up");

            sock.flush();

        }

And then here is the server:

function socketDataHandler(event:ProgressEvent):void{

var socket:Socket = event.target as Socket;

var message:String = socket.readUTF();

for each (var socket:Socket in clientSockets)

{

        socket.writeUTF(message);

        socket.flush();

}

}

And finally, here is the recieving client (I have a method that allows the server to differentiate between the two):

if(msg=="p1_down_down"){

        p1_down="down";

    }

    if(msg=="p1_down_up"){

        p1_down="up";

    }

    if(msg=="p1_up_down"){

        p1_down="down";

    }

    if(msg=="p1_up_up"){

        p1_down="up";

    }

Now many of you may already see the issue, as when the down key is up, it sends the message "p1_down_up". When the up key is up, it sends the message "p1_up_up". Both messages are sending at once when neither of them are being pressed. The receiving client is, I suppose, just getting one of the signals, or perhaps neither of them. How do I make MULTIPLE signals get wrote and read over the server? I tried using an array but you can't write those apparently. Someone please help, I've been reading allover the web for a week with absolutely no answer. Thank you.

TOPICS
ActionScript
493
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation