Search code examples
socketshaxeopenfl

Socket not receiving both messages from server (almost at same time)


So i got this issue here, I'm using a openfl.net.Socket to connect with my server and receive messages from it. The problem is the server is sending two messages almost at the same time and my socket appears to read only one, i tried putting a breakpoint on the second message and releasing it just after it stops (like a sleep of 0.5 seconds), and so my client receive both messages, but sending both at almost same time i get only one... Tips?

socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
function onResponse(e:ProgressEvent):Void
    {
        trace("response");
        if (socket.bytesAvailable > 0)
        {
            var size:Int = socket.readInt();
            var domainId:Int = socket.readInt();
            var messageId:Int = socket.readInt();
            var count:Int = socket.readInt();

            var socketData:String = socket.readUTFBytes(socket.bytesAvailable);
            trace("RECEIVE: " + socketData);

            var message:Message = Message.JSONToMessage(socketData);
            Domain.processMessage(message);
        }
    }

I hope i made myself clear


Solution

  • So, on the receiving side, in one recv you are getting all data that sent from the other end.

    One thing you should know about TCP is that it does not maintain message boundaries. It in fact does not know what a "message" is. Its a byte stream protocol. Three sends here will can result in three recv at the other end or even one recv at the other end for the full exchange of data.

    Applications using TCP should construct "messages" out of what the TCP is handing over to them. TCP just ensures the data is given in the order it was sent, and tries its best to deliver the packets sent to the receiver. It is up to the application protocol to define what should be done with the data.