AMF sockets out of sync when using ServerSocket in AIR
I have two (+) systems on a local network running an AIR application that need to send/receive updated positions and other events between each other. I'm currently using an AIR-based socket server one of the instances.
The AMF and strong typing works great. What doesn't, however, is the syncing between clients.
In my example below, clientA is trying to send an AMF packet to clientB. The SOCKET_DATA events fire off as expected on the server, in sync with the mouse, however the data that clientB receives very quickly gets out of sync and events are lost.
It's fine if an occasional event gets dropped, however I need to know the most recent SOCKET_DATA event has the latest data. Instead, it just contains increasingly older data.
I've posted a test build (with source) here:
http://dev.marcelray.com/send/SocketTest.zip
Any ideas or thoughts are greatly appreciated.
Thanks!
Code Snippets:
clientA writeObject: (on MOUSE_MOVE to test large number of events)
protected function stage_mouseMoveHandler ( pEvent : MouseEvent ) : void
{
var lOutgoingPacket : DataPacket = new DataPacket( pEvent.stageX , pEvent开发者_运维百科.stageY );
clientAStatusText.appendText( "Sending packet: " + lOutgoingPacket + "\n" );
clientA.writeObject( lOutgoingPacket );
}
clientB SOCKET_DATA handler:
private function clientB_socketDataHandler ( pEvent : ProgressEvent ) : void
{
var lIncomingPacket : DataPacket = DataPacket( clientB.readObject() );
clientBStatusText.appendText( "Received packet: " + lIncomingPacket + "\n" );
}
Server's SOCKET_DATA handler:
private function server_socketDataHandler ( pEvent : ProgressEvent ) : void
{
var lSourceClient : Socket = pEvent.target as Socket;
var lIncomingObject : * = lSourceClient.readObject();
// Send message to all connectedClients
for each ( var iClient : Socket in connectedClients )
{
// Don't send to client who sent the original command
if ( iClient == lSourceClient )
continue;
iClient.writeObject( lIncomingObject );
iClient.flush();
}
}
I don't think this is a problem with sync. The packets should always be in the correct order.
I'm pretty sure the problem is that you're assuming each call to Socket.writeObject
will be received as a single SOCKET_DATA
event, which is not necessarily true. What you need to do is send the length of the actual data along with the data. For example:
socket.writeUnsignedByte(lengthOfData);
socket.writeObject(data);
See this example for writing data:
https://github.com/magicalhobo/Flash-CS5-mobile-proxy/blob/master/com/magicalhobo/mobile/proxy/ProxyData.as
And socketDataHandler for reading data: https://github.com/magicalhobo/Flash-CS5-mobile-proxy/blob/master/com/magicalhobo/mobile/proxy/MobileClient.as
精彩评论