Instantly share code, notes, and snippets.

@beautyfree /new.js
Last active Dec 20, 2015

Embed
What would you like to do?
/**
Builds and returns the url of the socket transport endpoint
@method ioUrl
@return {String} The base url where our streaming api lives
**/
ioUrl : function() {
var cfg = (CL.Cfg.Node) ? CL.Cfg.Node : CL.Cfg.Stream;
return cfg.transport + '://' + cfg.host + ':' + cfg.port;
},
/**
Builds and returns the url used to connect to the socket. Includes
metadata used for auth
@method ioHandshakeUrl
@return {String} The url endpoint for the socket to connect to
**/
ioHandshakeUrl : function() {
var me = this,
CU = me.CurrentUser,
csrf = $('meta[name="csrf-token"]').attr('content');
return me.ioUrl() + '/room?id=' + CU.get('uuid') + '&session=' + md5(CU.get('uuid') + csrf);
},
/**
Initializes the socket connection, adds listeners and
tries to join the current room (if already connected)
@method connect
@return {Object} The current context
@chainable
**/
connect : function() {
var me = this;
if (me.CurrentUser && me.CurrentUser.get('uuid')) {
if (!me.io) {
var me = this;
me.io = io.connect(me.ioHandshakeUrl(), {
'transports' : ['websocket', 'flashsocket','xhr-polling'],
// 'connect timeout': 500, // Removing temporarily because it causes FF to connect using xhr-polling
'reconnect': true,
'reconnection delay': 500,
'reopen delay': 500,
'max reconnect attempts' : 5,
'force new connection' : true
});
me.io.on('join_success', $.proxy(me.onIOJoinSuccess, me));
me.io.on('connect', $.proxy(me.onIOConnect, me));
// me.io.on('booted', $.proxy(me.onIOBooted, me));
me.io.on('reconnecting', $.proxy(me.onIOReconnecting, me));
me.io.on('connect_failed', $.proxy(me.onIOConnectFailed, me));
me.io.on('disconnect', $.proxy(me.onIODisconnect, me));
me.io.on('error', $.proxy(me.onIOError, me));
CL.trigger('socketChange', me.io);
} else {
if (me.io.socket.connected == true) {
retry_attempts = 0;
if (retry_interval) {
clearInterval(retry_interval);
retry_interval = null;
}
me.joinRoom();
} else if (!me.io.socket.connected && !me.io.socket.connecting) {
me.io.socket.reconnect(null,{'force new connection':true});
}
}
}
return me;
},
ioUrl: function () {
var b = CL.Cfg.Node ? CL.Cfg.Node : CL.Cfg.Stream;
return b.transport + "://" + b.host + ":" + b.port
},
connect: function () {
var d = this;
try {
if (a)
if (d.io)!0 == d.io.socket.connected ? (b = 0, c && (clearInterval(c), c = null), d.joinRoom()) : !d.io.socket.connected && !d.io.socket.connecting && d.io.socket.reconnect(null, {
"force new connection": !0
});
else {
d = this;
d.io = io.connect(d.ioUrl() + "/room", {
transports: ["websocket", "flashsocket", "xhr-polling"],
reconnect: !0,
"reconnection delay": 500,
"reopen delay": 500,
"max reconnect attempts": 5
});
var e = d.io.emit;
d.io.emit = function () {
d.onIOEmit.apply(d, arguments);
e.apply(d.io, arguments)
};
var g = d.io.$emit;
d.io.$emit = function () {
d.onIOEvent.apply(d, arguments);
g.apply(d.io, arguments)
};
d.io.removeListener("join_success", $.proxy(d.onIOJoinSuccess, d));
d.io.removeListener("connect", $.proxy(d.onIOConnect,
d));
d.io.removeListener("reconnecting", $.proxy(d.onIOReconnecting, d));
d.io.removeListener("connect_failed", $.proxy(d.onIOConnectFailed, d));
d.io.removeListener("disconnect", $.proxy(d.onIODisconnect, d));
d.io.removeListener("error", $.proxy(d.onIOConnectFailed, d));
d.io.on("join_success", $.proxy(d.onIOJoinSuccess, d));
d.io.on("connect", $.proxy(d.onIOConnect, d));
d.io.on("reconnecting", $.proxy(d.onIOReconnecting, d));
d.io.on("connect_failed", $.proxy(d.onIOConnectFailed, d));
d.io.on("disconnect", $.proxy(d.onIODisconnect,
d));
d.io.on("error", $.proxy(d.onIOConnectFailed, d))
}
} catch (f) {
CL.log(f)
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment