A Demonstration of the ServerSocket API in AIR 2

In order to validate the new ServerSocket APIs in AIR 2, I wrote an application called HTTPeek. HTTPeek is a proxy server that sits between your browser and the network, and can show you HTTP request and response headers. It can handle compressed content, chunked content, binary content, etc. Check out the video below to see it in action:

Most of the HTTPeek code is dedicated to implementing just enough of the HTTP protocol to be an effective proxy, but the socket portion of the code is actually not all that complex. And the creation of the ServerSocket itself is very simple. The function below gets called when the user clicks on the "Listen" button:

private function onListen():void{this.sockets = new Array();this.server = new ServerSocket();this.server.addEventListener(Event.CONNECT, onConnect);try{this.server.bind(Number(this.portNumber.text), String(this.interfaces.value));this.server.listen();this.listenButton.label = "Close";}catch (e:Error){Alert.show(e.message, "Error", Alert.OK);return;}this.debugButton.enabled = true;}

HTTPeek is open-source and hosted here on Google Code. Feel free to check out the source to see how it works.

13 Responses to A Demonstration of the ServerSocket API in AIR 2

  1. Oh man oh man! Will this functionality be able to intercept and interpret AMF requests? This would be a FANTASTIC replacement for Flash debugger!

  2. Aran says:

    @Christian – gGreat work. I always like to see people doing things on the Flash platform outside of the standard Web / RIA space.@Andy – Service Capture and Charles are products which have been able to decrypt and show AMF0/3 traffic for some years now. We use Service Capture everyday for debugging our Flex apps

  3. Rytis says:

    Very cool. Does this new ServerSocker API mean that we can be create out own ActionScript based web server?

  4. Christian Cantrell says:

    @Andy: Tes, this should work fine. The AMF packets won’t automatically be decoded, but you can certain decode them. That’s an interesting idea. It looks @Aran has some good suggestions on how to do that with existing tools, but it’s always fun to write your own. :)@Rytis: You certainly can create your own web server. In fact, that’s what I was going to do initially. I was thinking of using Alchemy to compile PHP into ActionScript byte code, then write a thin web server on top of that to create a cross-platform PHP development environment. But with all the other AIR 2 features I needed to write apps for, I just didn’t have time, so I settled for an HTTP proxy.

  5. Rytis says:

    ActionScript is much better language than PHP and after tasting real OOP I would never come back to PHP again.Creating server side ActionScript API would be MUCH more interesting 🙂

  6. Raymond says:

    Hi Christian,I would really really like to see a demo of the web server that you have mentioned above – “using Alchemy to compile PHP into ActionScript byte code”Is this really possible with AIR 2.0?Please show me a demo with PHP, please 🙂

  7. Ian Drake says:

    I’m working on a conversion to Air right now using webkit. I’m really missing multi-threading though, so:One possibility:What thread services requests on the socket server?Another:Will the webkit update include “web workers”?

  8. y_nk says:

    Hello,i played with that part of the new API today, and get some issues.I did a very small server and client, with textfield, a msn-like application. The issue i’m on is a sandbox issue. When connecting, the server handles a new connection and i can read “” when reading data from the socket.i really don’t know what to do since an air application can’t have a crossdomain.xml filethanks for helping

  9. y_nk says:

    this may be usefull for anyone :I found how to fix the sandbox things, which is pretty normal :)You’ve to request a policy file on the client, and then send a bytearray containing the policy file from the server :http://blog.martian-arts.org/air-2-aka-athena-serversocket-crossdomain-needed/

  10. Breizo says:

    Hi CHristian,Great example. Thanks.I tried it, but I run into this one:VerifyError: Error #1014: Class flash.net::ServerSocket could not be found.I did install Air 2 runtime and Air 2 SDK, though…ANy idea? I use FB Pro 3Thanks

    • steve.ro says:

      Having the same problem, but with SecureSocket. Upgraded to FB4 just to see if that would fix it, but it did not. Did you find a solution? Any clues would be helpful, thanks.

  11. m1gu3l says:

    anyone tried making RTMP server on AIR?

  12. Göran says:

    I have bought CS5 and the AIR in FB4 seems to missing the type: ServerSocketConnectEvent is not declared.

    function onConnect(event:ServerSocketConnectEvent) -> gives error 1046.

    Any clue of what to do ?