Olivier J.
Olivier J.

Reputation: 3165

Connecting air/flash app to websocket server

I'm trying to connect my air application to a websocket server which is not on my domain. I found this code on GitHub : https://github.com/theturtle32/AS3WebSocket but I can not connect to server.

Here is my code :

<fx:Script>
    <![CDATA[
        import com.worlize.websocket.WebSocket;
        import com.worlize.websocket.WebSocketErrorEvent;
        import com.worlize.websocket.WebSocketEvent;

        private var websocket:WebSocket;

        private function handleCreationComplete():void 
        {
            websocket = new WebSocket("wss://echo.websocket.org", "*");

            websocket.debug = true;

            websocket.addEventListener(WebSocketEvent.CLOSED, handleWebSocketClosed);
            websocket.addEventListener(WebSocketEvent.OPEN, handleWebSocketOpen);
            websocket.addEventListener(WebSocketEvent.MESSAGE, handleWebSocketMessage);
            websocket.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
            websocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);
            websocket.addEventListener(WebSocketErrorEvent.CONNECTION_FAIL, handleConnectionFail);

            websocket.connect();
        }

        private function handleIOError(event:IOErrorEvent):void 
        {
            trace("error");
        }

        private function handleSecurityError(event:SecurityErrorEvent):void 
        {
            trace("sec error");
        }

        private function handleConnectionFail(event:WebSocketErrorEvent):void 
        {
            trace("connection error");
        }

        private function handleWebSocketClosed(event:WebSocketEvent):void 
        {
            trace("close");
        }

        private function handleWebSocketOpen(event:WebSocketEvent):void 
        {
            trace("connected !");
        }

        private function handleWebSocketMessage(event:WebSocketEvent):void 
        {
            trace("message !");
        }


    ]]>
</fx:Script>

Connecting to echo.websocket.org on port 443 Socket Connected starting SSL/TLS GET / HTTP/1.1

Host: echo.websocket.org

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Key: OCZu2AgYt0WgBcLFRV5EyQ==

Origin: *

Sec-WebSocket-Version: 13

Socket Disconnected close

Only my handleWebSocketClosed method is fired, my handleWebSocketOpen is never fired (weird...).

I can connect server with pur javascript so I know this is not a server problem.

In GitHub explanation project I can read :

THIS CLIENT WILL NOT WORK with draft-75 or draft-76/-00 servers that are deployed on the internet. It is only for the most recent RFC6455 standard

I don't know if this is the reason why I am failing to connect...

Is this a way to connect to a websocket server with as3 ?

Thanks and sorry for my poor english

Upvotes: 1

Views: 752

Answers (1)

AmigaAbattoir
AmigaAbattoir

Reputation: 749

Take a look at html5 Websocket with SSL, it seems like trying to connect via SSL requires some modification to make it use flash.net.SecureSocket. You may need to rewrite some of the AS3WebSocket to use that instead of flash.net.Socket.

Upvotes: 1

Related Questions