[AS3] Java-Flash communication with Sockets and AMF coding

Hello,
as you can see in title, I’m bothering with Java-Flash communication. At the beggining, I used XML Socket Class, which was pretty easy to implement and my aplication comunicated very well.

However, my program will monitor behavior of many users at the same time, so parsing and sending large xml files could overload Server. Thats why I used Socket class and AMF coding instead. After tough hours of implementing it, whole application seemed to work. Unfortunently, after couple of tests it apeard, that Socket was slower than XML Socket! I have no idea why, because in theory binary messages should be much faster than xml. Could you help me solving this issue? I would be thankfull for every advice and solution for this problem. Below I pasted my code for both Java and AS3.

This code sends basic variables from class object and are received the same way (it can sound bit complicated compared to just sending whole object, but for various reasons is the only way).

  1. Java client (java server receives data from different sources and sends it to flash):

    public void sendMyPosition(MyPositionMessage message, Socket sock) throws IOException
    {
         synchronized (sock) {

            SerializationContext context = SerializationContext.getSerializationContext();//getSerializationContext();

            OutputStream bout = sock.getOutputStream();
            Amf3Output amf3Output = new Amf3Output(context);
            amf3Output.setOutputStream(bout);
            amf3Output.writeInt(20);
            amf3Output.writeInt(0);
            amf3Output.writeFloat(message.getPosition().getPoint().getLattitude());
            amf3Output.writeFloat(message.getPosition().getPoint().getLongitude());
            amf3Output.writeFloat(message.getPosition().getPoint().getHeight());
            //amf3Output.write((byte)0);
            amf3Output.flush();

        }
    }
  1. Flash server - new data triggers socketDataHandler which receive it with readReasponse function. First int describes which kind of message was send, although there is only one at the moment. I’ve deleted some handlers couse they seemd irrelevant this case. Whole AS Class is attached as a file.
public class CustomSocket extends Socket {
        
        private var currentMessage:int = -1;
        private var message:Object = null;
        private var trackDraw:TrackDraw;
        
        public function CustomSocket(trackDraw:TrackDraw, host:String = null, port:uint = 0) {
            super(host, port);
            this.objectEncoding = 3;
            this.trackDraw = trackDraw;
            configureListeners();
        }
        
        private function configureListeners():void {
            addEventListener(Event.CLOSE, closeHandler);
            addEventListener(Event.CONNECT, connectHandler);
            addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
        }
        
        private function readResponse():void 
        {
            //var post:ByteArray = new ByteArray();
            //readBytes(post, 0, bytesAvailable);
            
            if( currentMessage < 0 ) currentMessage = readInt();
            else
            {
                if(currentMessage == BasicMessage.POSITION_MESSAGE_PARAM)
                    readPositionMessage();
                else currentMessage = -1;
            }
            
        }
        
        private function readPositionMessage():void
        {
            if( message == null) message = new PositionMessage();
            
            if( message.getId() == -1 ) message.setId(readInt());
            else if( message.getLattitude() == -1 ) message.setLattitude(readFloat());
            else if( message.getLongitude() == -1 ) message.setLongitude(readFloat());
            else if( message.getHeight() == -1 )
            {
                message.setHeight(readFloat());
                trackDraw.setRacerPosition(message as PositionMessage);
                currentMessage = -1;
                trace("Message: "+message.getId()+" "+message.getLattitude()+" "+message.getLongitude()+" "+message.getHeight());
                message = null;
            }
        }
        
        private function socketDataHandler(event:ProgressEvent):void {
            //trace("socketDataHandler: " + event);
            readResponse();
        }
}