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 );
}
});
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 );
}
});