by Christian Cantrell

 Comments (13)

Created

November 4, 2009

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.

COMMENTS

  • By andy matthews - 8:34 AM on November 4, 2009   Reply

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

  • By Aran - 5:29 PM on November 4, 2009   Reply

    @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

  • By Rytis - 5:55 AM on November 5, 2009   Reply

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

  • By Christian Cantrell - 6:07 AM on November 5, 2009   Reply

    @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.

  • By Rytis - 7:57 AM on November 5, 2009   Reply

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

  • By Raymond - 7:50 PM on November 9, 2009   Reply

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

  • By Ian Drake - 10:25 PM on November 9, 2009   Reply

    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”?

  • By y_nk - 9:08 AM on November 17, 2009   Reply

    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

  • By y_nk - 3:59 AM on November 18, 2009   Reply

    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/

  • By Breizo - 10:31 AM on January 18, 2010   Reply

    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

    • By steve.ro - 6:27 PM on December 8, 2010   Reply

      Breizo,
      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.

  • By m1gu3l - 9:49 AM on October 16, 2010   Reply

    anyone tried making RTMP server on AIR?

  • By Göran - 10:56 AM on April 12, 2011   Reply

    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 ?

ADD A COMMENT