Flex跨域套接字无法正常工作

时间:2013-01-28 17:24:32

标签: flash flex sockets cross-domain

我正在尝试通过浏览器与使用Flex / Flash在我的本地计算机上运行的第三方视频会议应用进行通信。用户可以登录我们的网页,该页面依次告诉本地应用程序打开并转到正确的聊天室。需要Flash作为绕过混合源警告的桥梁。我的AS代码:

    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();
    };

在本地运行一切顺利,'onProgress'监听器被触发,我看到应用程序响应请求。但是,从其他域运行会失败。我没有得到沙盒错误,就像呼叫没有发生一样。远程调试器表明连接成功。

你可以从代码中看到我是AS3的初学者,但在这里提出这个问题之前我做了很多研究。我希望有人能帮帮忙。在Flash Pro,Flash Builder(作为AS Project和Flex Project)中尝试了这个代码,结果相同。

1 个答案:

答案 0 :(得分:0)

看起来这是一个时间问题; mySocket.writeMultiByte调用发生在套接字准备好交换数据之前。在connecthandler中进行调用解决了这个问题。

感谢日志记录建议,真的很有帮助!

代码:

    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);