How to communicate between Air Action Script application and Node.js server

can someone please suggest way to communicate between Air(Action script) App and node.js server?

e.g.

  1. Communication between PHP and Flash(Action Script) application using AMFPHP

  2. BlazeDS for Java+Adobe Flex and Adobe Integrated Runtime (AIR)

Please send me your suggestions, any tutorial, or PoC sample code

Thanks in Advance.

Here is a sample I wrote for a blog. I think the code itself pretty much explains itself.


Client Side AS3 Code :

var urlString:String = "http://localhost:1337/"; 

function Submit():void
{
    var requestVars:URLVariables = new URLVariables();
    requestVars.Username = "guest";

    var request:URLRequest = new URLRequest();
    request.url = urlString;
    request.method = URLRequestMethod.GET;
    request.data = requestVars;

    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.TEXT;
    loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);

    try { loader.load(request); }

    catch (error:Error) { // Handle Immediate Errors }
}

function loaderCompleteHandler(e:Event):void
{
    trace(e.target.data); // Response Text
}

Brief about the code snippet:

  • We fill in the request data by instantiating a URLVariables class as requestVars.
  • We fill in the url & method by instantiating URLRequest class as request.
  • We attach an load complete event Handler to handle the response.
  • Wrapping up for catching errors, we call the load method.
  • URL is set to Localhost port 1337 where the nodeJS would be hosted.

  • The variable set is a test field UserName which is checked for in the server script.


Server Side NodeJS Code :

var http = require('http'), url = require('url');

http.createServer(function (req, res) {

  res.writeHead(200, {'Content-Type': 'text/plain'});

  var urlObj = url.parse(req.url, true);

  if(urlObj.query["Username"] == "guest") res.end("True");    

  else res.end("False");    

}).listen(1337, "127.0.0.1");

console.log('Server running at http://127.0.0.1:1337/');

Brief about the code snippet:

  • The code listens at port 1337 in the localhost server where it is hosted.
  • The query string is unwrapped to get the UserName variable & tested.
  • Server Responds with true since the value equates to guest.