PDA

View Full Version : Simple Adobe AIR XMLSocket wrapper class...



haibijon
7 Jul 2007, 2:25 PM
Hey guys, not sure if anyone else is currently using the XMLSocket class in AIR, but I've found Adobe's procedural/asynchronous style to be pretty gross. Using Jack's SqlDB example I wrapped the AIR XMLSocket class. Let me know what you guys think, so far this is working great for me, but code critique is always greatly appreciated.

Thanks!



Ext.namespace("Ext.air");

Ext.air.SocketConnection = function(config){
Ext.apply(this, config);
Ext.air.SocketConnection.superclass.constructor.call(this);

this.addEvents({
open : true,
data : true,
ioerror : true,
securityerror : true,
close: true
});

this.connected = false;
};

Ext.extend(Ext.air.SocketConnection, Ext.util.Observable, {

_newLine : "\n",

open : function(cb, scope){
this.conn = new air.XMLSocket();

this.conn.addEventListener(air.IOErrorEvent.IO_ERROR, this.onIOError.createDelegate(this) );
this.conn.addEventListener(air.SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError.createDelegate(this) );
this.conn.addEventListener(air.DataEvent.DATA, this.onData.createDelegate(this) );
this.conn.addEventListener(air.Event.CONNECT, this.onOpen.createDelegate(this, [cb, scope]) );
this.conn.addEventListener(air.Event.CLOSE, this.onClose.createDelegate(this) );

this.conn.connect( this.host, this.port );
},

close : function(){
this.conn.close();
this.connected = true;
},

onData : function(e){
this.fireEvent('data', e.data, this);
},

onOpen : function(cb, scope){
this.connected = true;
Ext.callback(cb, scope, [this]);
this.fireEvent('open', this);
},

onClose : function(){
this.fireEvent('close', this);
},

onIOError : function(e, type){
this.connected = false;
this.fireEvent( "ioerror", e );
},

onSecurityError : function(e, type){
this.connected = false;
this.fireEvent( "securityerror", e );
},

sendln : function( o ){
this.send( o + this._newLine );
},

send : function( o ){
this.conn.send( o );
}
});

Wolfgang
4 Oct 2007, 2:23 AM
Nice class. :)
I was doing something similar with ALFAX before.

Have you tried it with the new AIR beta2 with respect to the new security model using two different sandboxes and a sandboxBridge?

Regards

Wolfgang

dudecool
15 Apr 2009, 5:11 AM
Hi,

Do you have VB or C# implementation of server?, i need to know how its done @ server side, i am trying it with Socket class in C# with that i can send data from AIR client to C# server but not the opposite, any help is appreciated :)
Regards,
Jayaveer.B

Wolfgang
7 May 2009, 1:08 AM
I am not using Windows Servers but Linux , so no.
Anyway there are a lot of Server Samples out written in PERL, that work really nicely.

Basically you open a (TCP)socket, and listen to incoming requests. I.e. from your JS world using AFLAX, AIR etc.
Then your servers sends some response that you process as any other event in the JS world.
Usally these responses need to be terminated with \0 to indicate the end. (not \n)

The more complex part is if you want to manage multiple different connection on the serverside and have commands/subcommands implemented. Here you might need to create different threads or processes and must make sure to avoid deadlocks.

Regards

Wolfgang

dudecool
8 May 2009, 4:43 AM
Holy crap..., there was no problem with my code! all i missed was "\0" just 2 characters bugged me from past 1 month :(, is there any documentation that it needs to end with "\0"?
Thanks for the info :).
I am trying to make an intranet chat application using AIR and C# server, once done i will post it, any ideas and suggestions are welcomed :)