Skip to content

Instantly share code, notes, and snippets.

@piroor
Last active March 25, 2018 20:29
Show Gist options
  • Select an option

  • Save piroor/5185096 to your computer and use it in GitHub Desktop.

Select an option

Save piroor/5185096 to your computer and use it in GitHub Desktop.
Wrapper of socket.io-client for Sencha Touch/ExtJS.
/**
* Usage:
* var socket = Ext.create('MyApp.data.SocketIO', {
* host: 'localhost',
* port: 80
* });
*
* License: The MIT License
* Copyright (c) 2013 ClearCode Inc. / YUKI "Piro" Hiroshi
*/
Ext.define('MyApp.data.SocketIO', {
constructor: function(config){
var me = this;
me.superclass.constructor.call(me);
me.self._sockets = me.self._sockets || {};
var key = config.host + ':' + config.port;
if (!me.self._sockets[key]) {
me.self._sockets[key] = me.connect(config);
}
me._socket = me.self._sockets[key];
},
connect: function(config) {
return io.connect(config.host, {
port: config.port,
path: config.path,
reconnection: config.reconnection,
reconnectionAttempts: config.reconnectionAttempts,
reconnectionDelay: config.reconnectionDelay,
reconnectionDelayMax: config.reconnectionDelayMax,
timeout: config.timeout
});
},
on: function() {
return this._socket.on.apply(this._socket, arguments);
},
removeListener: function() {
return this._socket.removeListener.apply(this._socket, arguments);
},
removeAllListener: function() {
return this._socket.removeAllListener.apply(this._socket, arguments);
},
emit: function() {
return this._socket.emit.apply(this._socket, arguments);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment