App-SocialCalc-Multiplayer
view release on metacpan or search on metacpan
socialcalc/third-party/Socket.IO-node/support/socket.io-client/socket.io.js view on Meta::CPAN
* it will automatically update the timeout, decode the message and
* forwards the response to the onMessage function for further processing.
*
* @param {String} data Response from the server.
* @api private
*/
Transport.prototype.onData = function(data){
this.setTimeout();
var msgs = this.decode(data);
if (msgs && msgs.length){
for (var i = 0, l = msgs.length; i < l; i++){
this.onMessage(msgs[i]);
}
}
};
/**
* All the transports have a dedicated timeout to detect if
* the connection is still alive. We clear the existing timer
* and set new one each time this function is called. When the
* timeout does occur it will call the `onTimeout` method.
*
* @api private
*/
Transport.prototype.setTimeout = function(){
var self = this;
if (this.timeout) clearTimeout(this.timeout);
this.timeout = setTimeout(function(){
self.onTimeout();
}, this.options.timeout);
};
/**
* Disconnect from the Socket.IO server when a timeout occurs.
*
* @api private
*/
Transport.prototype.onTimeout = function(){
this.onDisconnect();
};
/**
* After the response from the server has been parsed to individual
* messages we need to decode them using the the Socket.IO message
* protocol: <https://github.com/learnboost/socket.io-node/>.
*
* When a message is received we check if a session id has been set,
* if the session id is missing we can assume that the received message
* contains the sessionid of the connection.
* When a message is prefixed with `~h~` we dispatch it our heartbeat
* processing method `onHeartbeat` with the content of the heartbeat.
*
* When the message is prefixed with `~j~` we can assume that the contents
* of the message is JSON encoded, so we parse the message and notify
* the base of the new message.
*
* If none of the above, we consider it just a plain text message and
* notify the base of the new message.
*
* @param {String} message A decoded message from the server.
* @api private
*/
Transport.prototype.onMessage = function(message){
if (!this.sessionid){
this.sessionid = message;
this.onConnect();
} else if (message.substr(0, 3) == '~h~'){
this.onHeartbeat(message.substr(3));
} else if (message.substr(0, 3) == '~j~'){
this.base.onMessage(JSON.parse(message.substr(3)));
} else {
this.base.onMessage(message);
}
},
/**
* Send the received heartbeat message back to server. So the server
* knows we are still connected.
*
* @param {String} heartbeat Heartbeat response from the server.
* @api private
*/
Transport.prototype.onHeartbeat = function(heartbeat){
this.send('~h~' + heartbeat); // echo
};
/**
* Notifies the base when a connection to the Socket.IO server has
* been established. And it starts the connection `timeout` timer.
*
* @api private
*/
Transport.prototype.onConnect = function(){
this.connected = true;
this.connecting = false;
this.base.onConnect();
this.setTimeout();
};
/**
* Notifies the base when the connection with the Socket.IO server
* has been disconnected.
*
* @api private
*/
Transport.prototype.onDisconnect = function(){
this.connecting = false;
this.connected = false;
this.sessionid = null;
this.base.onDisconnect();
};
/**
* Generates a connection url based on the Socket.IO URL Protocol.
* See <https://github.com/learnboost/socket.io-node/> for more details.
*
* @returns {String} Connection url
* @api private
*/
Transport.prototype.prepareUrl = function(){
socialcalc/third-party/Socket.IO-node/support/socket.io-client/socket.io.js view on Meta::CPAN
return this;
};
/**
* Queues messages when there isn't a active connection available. Once a connection has been
* established you should call the `doQueue` method to send the queued messages to the server.
*
* @param {Mixed} message The message that was originally send to the `send` method.
* @returns {io.Socket}
* @api private
*/
Socket.prototype.queue = function(message){
if (!('queueStack' in this)) this.queueStack = [];
this.queueStack.push(message);
return this;
};
/**
* If there are queued messages we send all messages to the Socket.IO server and empty
* the queue.
*
* @returns {io.Socket}
* @api private
*/
Socket.prototype.doQueue = function(){
if (!('queueStack' in this) || !this.queueStack.length) return this;
this.transport.send(this.queueStack);
this.queueStack = [];
return this;
};
/**
* Check if we need to use cross domain enabled transports. Cross domain would
* be a different port or different domain name.
*
* @returns {Boolean}
* @api private
*/
Socket.prototype.isXDomain = function(){
var locPort = window.location.port || 80;
return this.host !== document.domain || this.options.port != locPort;
};
/**
* When the transport established an working connection the Socket.IO server it notifies us
* by calling this method so we can set the `connected` and `connecting` properties and emit
* the connection event.
*
* @api private
*/
Socket.prototype.onConnect = function(){
this.connected = true;
this.connecting = false;
this.doQueue();
if (this.options.rememberTransport) this.options.document.cookie = 'socketio=' + encodeURIComponent(this.transport.type);
this.emit('connect');
};
/**
* When the transport receives new messages from the Socket.IO server it notifies us by calling
* this method with the decoded `data` it received.
*
* @param data The message from the Socket.IO server.
* @api private
*/
Socket.prototype.onMessage = function(data){
this.emit('message', [data]);
};
/**
* When the transport is disconnected from the Socket.IO server it notifies us by calling
* this method. If we where connected and the `reconnect` is set we will attempt to reconnect.
*
* @api private
*/
Socket.prototype.onDisconnect = function(){
var wasConnected = this.connected;
this.connected = false;
this.connecting = false;
this.queueStack = [];
if (wasConnected){
this.emit('disconnect');
if (this.options.reconnect && !this.reconnecting) this.onReconnect();
}
};
/**
* The reconnection is done using an exponential back off algorithm to prevent
* the server from being flooded with connection requests. When the transport
* is disconnected we wait until the `reconnectionDelay` finishes. We multiply
* the `reconnectionDelay` (if the previous `reconnectionDelay` was 500 it will
* be updated to 1000 and than 2000>4000>8000>16000 etc.) and tell the current
* transport to connect again. When we run out of `reconnectionAttempts` we will
* do one final attempt and loop over all enabled transport methods to see if
* other transports might work. If everything fails we emit the `reconnect_failed`
* event.
*
* @api private
*/
Socket.prototype.onReconnect = function(){
this.reconnecting = true;
this.reconnectionAttempts = 0;
this.reconnectionDelay = this.options.reconnectionDelay;
var self = this
, tryTransportsOnConnectTimeout = this.options.tryTransportsOnConnectTimeout
, rememberTransport = this.options.rememberTransport;
function reset(){
if(self.connected) self.emit('reconnect',[self.transport.type,self.reconnectionAttempts]);
self.removeEvent('connect_failed', maybeReconnect).removeEvent('connect', maybeReconnect);
self.reconnecting = false;
delete self.reconnectionAttempts;
delete self.reconnectionDelay;
delete self.reconnectionTimer;
delete self.redoTransports;
self.options.tryTransportsOnConnectTimeout = tryTransportsOnConnectTimeout;
self.options.rememberTransport = rememberTransport;
return;
};
( run in 0.582 second using v1.01-cache-2.11-cpan-39bf76dae61 )