Question

When using flash.net.Socket class to connect to remote server, I encountered a strange situation. When writing socket directly in Flash Professional layer's code space, persistent connection can be established, and be established for only ONCE. However, if I wrapped that connection logic into a class, the class instance is constantly creating NEW sockets like crazy.

So here is my code when running socket standalone:

var host:String = 'xx.xx.xx.xx';
var port:Number = 123456;
trace("host:"+host);
trace("port:"+port);

if (true){
    var socket:Socket = new Socket(host, port);
    socket.addEventListener(Event.CONNECT, onConnect);
    socket.addEventListener(Event.CLOSE, onClose);
    socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
    socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
    socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);

    function onConnect(e:Event):void {
        trace("Connected to socket...");
        Log.info("Connected to socket...");
    }

    function onClose(e:Event):void {
        // Security error is thrown if this line is excluded
        trace("Closing socket...");
        Log.info("Closing socket...");
        socket.close();
    }

    function onError(e:IOErrorEvent):void {
        trace("IO Error: "+e);
        Log.info("IO Error: "+e);
    }

    function onSecError(e:SecurityErrorEvent):void {
        trace("Security Error: "+e);
        Log.info("Security Error: "+e);
    }

    function onResponse(e:ProgressEvent):void {
        if (socket.bytesAvailable>0) {
            var res:String = socket.readUTFBytes(socket.bytesAvailable);
            trace(res);
            Log.info(res);
        }
    }
}

And here is the code with socket in a class:

package com.example {
    import flash.net.Socket;
    import flash.events.*;

    import com.example.utils.Log;

    public class MockSession {
        private var socket:Socket;

        public function MockSession(host:String,port:Number):void{
            socket = new Socket(host,port);
            socket.addEventListener(Event.CONNECT, onConnect);
            socket.addEventListener(Event.CLOSE, onClose);
            socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
            socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
            socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
        }

        private function onConnect(e:Event):void {
            trace("Connected to socket...");
            Log.info("Connected to socket...");
        }

        private function onClose(e:Event):void {
            // Security error is thrown if this line is excluded
            trace("Closing socket...");
            Log.info("Closing socket...");
            socket.close();
        }

        private function onError(e:IOErrorEvent):void {
            trace("IO Error: "+e);
            Log.info("IO Error: "+e);
        }

        private function onSecError(e:SecurityErrorEvent):void {
            trace("Security Error: "+e);
            Log.info("Security Error: "+e);
        }

        private function onResponse(e:ProgressEvent):void {
            if (socket.bytesAvailable>0) {
                var res:String = socket.readUTFBytes(socket.bytesAvailable);
                trace(res);
                Log.info(res);
            }
        }
    }
}

And how it is run in Flash code layer:

import com.example.MockSession;

var session2:MockSession = new MockSession('xx.xx.xx.xx', 123456);

This is producing a bunch of "socket connected" msg...

Connected to socket...
Connected to socket...
Connected to socket...
Connected to socket...
Connected to socket...
Connected to socket...
Connected to socket...
Connected to socket...
......

I can't figure out how using class could be in any difference. Can anyone help me?

Was it helpful?

Solution

Your code seems to be alright. But to be extra sure:

  • In the onConnect() handler function add the removeEventListener for the connection - this way you'll be sure that it's not the same instance of the socket that triggers the event. You can re-add the listener on the onClose() listener.
  • Add a break-point to either the constructor of your MockSession class or the onConnect() function and run the app in debug mode. That way you'll pause the app every time that line executes and you'll see the stack - what caused that line to execute. Perhaps you'll notice that there's multiple sockets being opened or there's a condition where it can be created again.

Finally, you might add a singleton entry to your MockSession class to be sure there's always only one instance (of course, if you don't plan to open multiple sockets at a time).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top