mirror of
https://github.com/socketio/socket.io.git
synced 2026-04-30 03:00:39 -04:00
217 lines
4.3 KiB
JavaScript
217 lines
4.3 KiB
JavaScript
|
|
/**
|
|
* socket.io
|
|
* Copyright(c) 2011 LearnBoost <dev@learnboost.com>
|
|
* MIT Licensed
|
|
*/
|
|
|
|
(function (exports, io) {
|
|
|
|
/**
|
|
* Expose constructor.
|
|
*/
|
|
|
|
exports.Transport = Transport;
|
|
|
|
/**
|
|
* This is the transport template for all supported transport methods.
|
|
*
|
|
* @constructor
|
|
* @api public
|
|
*/
|
|
|
|
function Transport (socket, sessid) {
|
|
this.socket = socket;
|
|
this.sessid = sessid;
|
|
};
|
|
|
|
/**
|
|
* Apply EventEmitter mixin.
|
|
*/
|
|
|
|
io.util.mixin(Transport, io.EventEmitter);
|
|
|
|
/**
|
|
* Handles the response from the server. When a new response is received
|
|
* 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.clearCloseTimeout();
|
|
|
|
if (data !== '') {
|
|
// todo: we should only do decodePayload for xhr transports
|
|
var msgs = io.parser.decodePayload(data);
|
|
|
|
if (msgs && msgs.length) {
|
|
for (var i = 0, l = msgs.length; i < l; i++) {
|
|
this.onPacket(msgs[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return this;
|
|
};
|
|
|
|
/**
|
|
* Handles packets.
|
|
*
|
|
* @api private
|
|
*/
|
|
|
|
Transport.prototype.onPacket = function (packet) {
|
|
if (packet.type == 'heartbeat') {
|
|
return this.onHeartbeat();
|
|
}
|
|
|
|
if (packet.type == 'disconnect' && packet.endpoint == '') {
|
|
return this.onDisconnect();
|
|
}
|
|
|
|
if (packet.type == 'connect' && packet.endpoint == ''){
|
|
return this.onConnect();
|
|
}
|
|
|
|
this.socket.onPacket(packet);
|
|
return this;
|
|
};
|
|
|
|
/**
|
|
* Sets close timeout
|
|
*
|
|
* @api private
|
|
*/
|
|
|
|
Transport.prototype.setCloseTimeout = function () {
|
|
if (!this.closeTimeout) {
|
|
var self = this;
|
|
|
|
this.closeTimeout = setTimeout(function () {
|
|
self.onDisconnect();
|
|
}, this.socket.closeTimeout);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Called when transport disconnects.
|
|
*
|
|
* @api private
|
|
*/
|
|
|
|
Transport.prototype.onDisconnect = function () {
|
|
if (this.close) this.close();
|
|
this.clearTimeouts();
|
|
this.socket.onDisconnect();
|
|
return this;
|
|
};
|
|
|
|
/**
|
|
* Called when transport connects
|
|
*
|
|
* @api private
|
|
*/
|
|
|
|
Transport.prototype.onConnect = function () {
|
|
this.socket.onConnect();
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Clears close timeout
|
|
*
|
|
* @api private
|
|
*/
|
|
|
|
Transport.prototype.clearCloseTimeout = function () {
|
|
if (this.closeTimeout) {
|
|
clearTimeout(this.closeTimeout);
|
|
this.closeTimeout = null;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Clear timeouts
|
|
*
|
|
* @api private
|
|
*/
|
|
|
|
Transport.prototype.clearTimeouts = function () {
|
|
this.clearCloseTimeout();
|
|
|
|
if (this.reopenTimeout) {
|
|
clearTimeout(this.reopenTimeout);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Sends a packet
|
|
*
|
|
* @param {Object} packet object.
|
|
* @api private
|
|
*/
|
|
|
|
Transport.prototype.packet = function (packet) {
|
|
this.send(io.parser.encodePacket(packet));
|
|
};
|
|
|
|
/**
|
|
* 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.packet({ type: 'heartbeat' });
|
|
};
|
|
|
|
/**
|
|
* Called when the transport opens.
|
|
*
|
|
* @api private
|
|
*/
|
|
|
|
Transport.prototype.onOpen = function () {
|
|
this.clearCloseTimeout();
|
|
this.socket.onOpen();
|
|
};
|
|
|
|
/**
|
|
* Notifies the base when the connection with the Socket.IO server
|
|
* has been disconnected.
|
|
*
|
|
* @api private
|
|
*/
|
|
|
|
Transport.prototype.onClose = function () {
|
|
this.reopenTimeout = setTimeout(function () {
|
|
this.open();
|
|
}, this.socket.options['reopen delay']);
|
|
|
|
this.setCloseTimeout();
|
|
this.socket.onClose();
|
|
};
|
|
|
|
/**
|
|
* 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 () {
|
|
return this.socket.options.resource + '/' + io.protocol
|
|
+ '/' + this.name + '/' + this.sessid;
|
|
};
|
|
|
|
})(
|
|
'undefined' != typeof io ? io : module.exports
|
|
, 'undefined' != typeof io ? io : module.parent.exports
|
|
);
|