Problem with Air2 ServerSocket Crossdomain

I want to create an Air 2 server through the ServerSocket class (below you will find sample code).

Limitations:

  • The server must be in the air
  • Client must be displayed through a web browser

Clients are displayed in a web browser, so when a client wants to establish a connection with an Air server, Flash sends a crossdomain request through the socket, and the server sends it back, but nothing happens.

As3Doc states that when flash sends a crossdomain request, the server should send it back, then Flash closes the connection and opens a new connection if crossdomain is ok.

I tried different settings, but nothing works, the client never receives the CONNECTED 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 Code:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.SecurityErrorEvent;
    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);
        }
    }
}
+3
1

, writeUTF XML . writeUTF UTF. XML, .

writeUTFBytes writeUTF, .

, writeByte, . null .

+2

All Articles