I am developing communication via Socket from my program with the server. This server is responsible for bridging between my program and the users who are connected to it, ie the user making a change in "program A", the user of "program B" will receive this change and apply the changes automatically. The number of connected programs is undefined. Below is a diagram illustrating:
Whereinredisthesendingofbytesandinblueisthereceipt.Theconnectionbetweentheserverandtheclientisuniqueanddoesnotclose.Wheretherearetwobluearrows,thetwochangesaresentinred.
Everythingworkscorrectly.TheproblemarosewhenIwassendingalargernumberofbytesthanIwastesting,andIrealizedthattheSocketobject"cut" sending bytes to the server, separating it into 2 packets, was received in two parts.
It happens that these bytes are part of a single JSON object, and upon receiving the packets, the ProgressEvent.SOCKET_DATA
event is dispatched twice, with the first and second part of the JSON object.
The shipping code is:
function enviar(json:Object):void {
var str:String = JSON.stringify(json); // transformo json em string
socket.writeUTFBytes(str); // escrevo a string no objeto socket
socket.flush(); // faço o envio
}
On receiving (This function is executed 2 times, "cutting" the bytes in two packages):
function receber(e:ProgressEvent.SOCKET_DATA):void {
var str:String = socket.readUTFBytes(socket.bytesAvailable); // transformo os bytes em string
var json:Object = JSON.parse(str);
}
Is there any limitation on sending the amount of bytes? Should I perform queue management of packages and separate them by processes? Does it happen in any language?