开发者

Air 2 ServerSocket Crossdomain problem

I wish to make an Air 2 Server via ServerSocket class (you will find below code an example about it).

Constraints :

  • Server must be in Air
  • Client must be displayed through web browser

Clients are displayed with an Web browser so when a client want to establish a connection to Air server, Flash sends a crossdomain request through socket and server sends it back but nothing hapen then.

The As3Doc specifies that when flash sends crossdomain request, server must to send it back, then Flash closes connection and open a new connection if crossdomain is ok.

I've tried different settings but nothing works, client never receives CONNECTED's event.

Any ideas ?

Server side code :

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init()">

    <s:TextArea x="0" y="0" width="100%" height="100%" id="log"/>

    <fx:Script>
        <![CDATA[
            private var _server : ServerSocket = new ServerSocket;

            private function init() : void
            {
                _server.bind(4500, "127.0.0.1");
                _server.addEventListener(ServerSocketConnectEvent.CONNECT, onClientConnection);
                _server.listen();
            }

            private function onClientConnection(e : ServerSocketConnectEvent) : void
            {
                var socket : Socket = e.socket;
                log.appendText("Client connected : " + socket.localAddress + ":" + socket.localPort + "\n");
                socket.addEventListener(ProgressEvent.SOCKET_DATA, onData);
            }

            private function onData(e:Event) : void
            {
                var socket : Socket = e.target as Socket;
                log.appendText("Data : " + socket.readUTFBytes(socket.bytesAvailable));
                socket.writeUTF(
                    '<cross-domain-policy>' +
                    '       <allow-access-from domain="*" to-ports="4500" />' +
                    '</cross-domain-policy>'
                    + String.fromCharCode(0)
                    );
                socket.writeByte(0);
                socket.flush();
                socket.close();
            }
        ]]>
    </fx:Script>
</s:WindowedApplication>

Client side code :

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.SecurityErrorEvent;
  开发者_如何学Go  import flash.net.Socket;
    import flash.text.TextField;
    import flash.utils.setTimeout;

    public class TestClient extends Sprite
    {
        private var log : TextField;
        private var _socket : Socket;

        public function TestClient()
        {
            log = new TextField;
            log.width = stage.stageWidth;
            log.height = stage.stageHeight;
            addChild(log);

            _socket = new Socket;
            _socket.addEventListener(Event.CONNECT, onConnection);
            _socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
            _socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
            tryConnection();
        }

        private function tryConnection() : void
        {
            log.appendText("Try connection ... \n");
            _socket.connect("127.0.0.1", 4500);
        }

        private function onConnection(e : Event) : void
        {
            log.appendText("Connected !");
        }

        private function onError(e : Event) : void
        {
            log.appendText(e.toString() + "\n");
            setTimeout(tryConnection, 1000);
        }
    }
}


Your approach is right, but you're using writeUTF to write down the XML policy file to the socket. writeUTF also writes the length of the string in two bytes before the actual UTF string. That is corrupting the XML policy file the client is receiving.

Simply use writeUTFBytes instead of writeUTF and everything should work just fine.

Also, you don't need to writeByte like you do. Appending a null character to your policy string is enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜