« Some Interesting AIR Marketplace Statistics | Main | A Demonstration of the New Storage Volume APIs in AIR 2 »
November 4, 2009
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.
Posted by cantrell at November 4, 2009 8:20 AM
Comments
Oh man oh man! Will this functionality be able to intercept and interpret AMF requests? This would be a FANTASTIC replacement for Flash debugger!
Posted by: andy matthews at November 4, 2009 8:34 AM
@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
Posted by: Aran at November 4, 2009 5:29 PM
Very cool. Does this new ServerSocker API mean that we can be create out own ActionScript based web server?
Posted by: Rytis at November 5, 2009 5:55 AM
@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.
Posted by: Christian Cantrell at November 5, 2009 6:07 AM
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 :)
Posted by: Rytis at November 5, 2009 7:57 AM
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 :)
Posted by: Raymond at November 9, 2009 7:50 PM
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"?
Posted by: Ian Drake at November 9, 2009 10:25 PM
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 file
thanks for helping
Posted by: y_nk at November 17, 2009 9:08 AM
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/
Posted by: y_nk at November 18, 2009 3:59 AM
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 3
Thanks
Posted by: Breizo at January 18, 2010 10:31 AM
Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy (updated 07-14-2009).