1

I'm trying to communicate from the browser with a 3rd party video conferencing app running on my local machine using Flex/Flash. The idea being that users can login on our webpage, the page in turn tells the local app to open and go to the proper chat room. Flash is needed as a bridge to bypass the mixed sources warning. My AS code:

    public function Check():void {  
        import flash.events.SecurityErrorEvent;
        import flash.external.ExternalInterface;
        import flash.net.Socket;
        import flash.system.Security;

        var mySocket:Socket = new Socket();
        Security.loadPolicyFile('xmlsocket://127.0.0.1:63457');

        mySocket.addEventListener(Event.CONNECT, onConnect);
        mySocket.addEventListener(Event.CLOSE,onClose);
        mySocket.addEventListener(IOErrorEvent.IO_ERROR, onError);
        mySocket.addEventListener(ProgressEvent.SOCKET_DATA, onProgress);
        mySocket.addEventListener(DataEvent.DATA, onData);
        mySocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);

        function onConnect(event:Event):void {
            trace('connect');
        }

        function onClose(event:Event):void {
            trace('close');
        }

        function onProgress(event:ProgressEvent):void {
            trace('Progressevent fired');
            var output:String = mySocket.readUTFBytes(mySocket.bytesAvailable);
            ExternalInterface.call("alert", output);
        }

        function onData(event:DataEvent):void {
            trace('onData fired');

        }

        function onSecurityError(event:ErrorEvent) : void {
            trace("SECURITY ERROR : " +  event.text);
            ExternalInterface.call("alert", 'error: ' + event.text, 0);
        }

        function onError(event:ErrorEvent) : void {
            trace("ERROR : " +  event.text);
            ExternalInterface.call("alert", 'error: ' + event.text, 0);
        }

        mySocket.connect("127.0.0.1", 63457);
        mySocket.writeMultiByte('GET /?url=http://app.mydomain.com HTTP/1.1', 'UTF-8');
        mySocket.flush();
    };

Running locally all goes well and the 'onProgress' listener is fired and I see the app responding to the request . Running from a different domain however fails. I do not get sandbox errors, it's just like the call isn't happening. Remote debugger suggest that the connection is made successfully.

As you can probably see from the code I am a beginner at AS3 but I did quite a bit of research before asking this question here. I hope someone can help. Tried this code in Flash Pro, Flash Builder (both as AS Project and Flex Project), same results.

Richard
  • 36
  • 5
  • Please take a look at [this post][1] [1]: http://stackoverflow.com/questions/2181320/help-with-cross-domain-file-for-actionscript-3-socket – Mikhail Payson Jan 28 '13 at 17:27
  • Is there a crossdomain.xml file in the root of the domain you're trying to access? When you "running locally" do you mean you have a web server setup on your local machine? Or that you are running the SWF from the file system? Are you creating a browser based app targeting Flash Player or a desktop app targeting Adobe AIR? – JeffryHouser Jan 28 '13 at 17:36
  • Running locally: yes when debugging (from file://) all goes well. I'm targeting Flash Player. The videoconf app that's running on localhost serves a crossdomain.xml: `$ echo -ne '\0' | nc -v 127.0.0.1 63457 Connection to 127.0.0.1 63457 port [tcp/*] succeeded! ` – Richard Jan 28 '13 at 17:47
  • Btw, when I change the location of the crossdomain.xml in the loadPolicyFile call I do see an error in the remote debugger so I guess that part is done the right way. – Richard Jan 28 '13 at 17:51

1 Answers1

0

It looks like this was a timing issue; The mySocket.writeMultiByte call happened before the socket was ready to exchange data. Making the call in the connecthandler solved the issue.

Thanks for the logging suggestion, really helped!

Code:

    import flash.events.SecurityErrorEvent;
    import flash.events.HTTPStatusEvent;
    import flash.external.ExternalInterface;
    import flash.net.Socket;
    import flash.system.Security;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    var mySocket:Socket = new Socket();

    mySocket.addEventListener(Event.CONNECT, onConnect);
    mySocket.addEventListener(Event.CLOSE,onClose);
    mySocket.addEventListener(IOErrorEvent.IO_ERROR, onError);
    mySocket.addEventListener(ProgressEvent.SOCKET_DATA, onProgress);
    mySocket.addEventListener(DataEvent.DATA, onSocketData);
    mySocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);

    function onConnect(event:Event):void
    {
        trace('connect');
        // ready to send!
        mySocket.writeMultiByte('GET /?url=http://app.mydomain.com HTTP/1.1', 'UTF-8');
    }

    function onClose(event:Event):void
    {
        trace('close');
    }

    function onProgress(event:ProgressEvent):void
    {
        var output:String = mySocket.readUTFBytes(mySocket.bytesAvailable);
        trace('Progressevent fired: \n' + output + '\n-----------');

    }

    function onSocketData(event:DataEvent):void
    {
        trace('onSocketData fired');

    }

    function onSecurityError(event:ErrorEvent):void
    {
        trace("SECURITY ERROR : " +  event.text);
    }

    function onError(event:ErrorEvent):void
    {
        trace("ERROR : " +  event.text);
    }

    mySocket.connect("127.0.0.1", 63457);
Richard
  • 36
  • 5