3 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
7 (function (exports, io) {
13 exports.Transport = Transport;
16 * This is the transport template for all supported transport methods.
22 function Transport (socket, sessid) {
28 * Apply EventEmitter mixin.
31 io.util.mixin(Transport, io.EventEmitter);
35 * Indicates whether heartbeats is enabled for this transport
40 Transport.prototype.heartbeats = function () {
45 * Handles the response from the server. When a new response is received
46 * it will automatically update the timeout, decode the message and
47 * forwards the response to the onMessage function for further processing.
49 * @param {String} data Response from the server.
53 Transport.prototype.onData = function (data) {
54 this.clearCloseTimeout();
56 // If the connection in currently open (or in a reopening state) reset the close
57 // timeout since we have just received data. This check is necessary so
58 // that we don't reset the timeout on an explicitly disconnected connection.
59 if (this.socket.connected || this.socket.connecting || this.socket.reconnecting) {
60 this.setCloseTimeout();
64 // todo: we should only do decodePayload for xhr transports
65 var msgs = io.parser.decodePayload(data);
67 if (msgs && msgs.length) {
68 for (var i = 0, l = msgs.length; i < l; i++) {
69 this.onPacket(msgs[i]);
83 Transport.prototype.onPacket = function (packet) {
84 this.socket.setHeartbeatTimeout();
86 if (packet.type == 'heartbeat') {
87 return this.onHeartbeat();
90 if (packet.type == 'connect' && packet.endpoint == '') {
94 if (packet.type == 'error' && packet.advice == 'reconnect') {
98 this.socket.onPacket(packet);
109 Transport.prototype.setCloseTimeout = function () {
110 if (!this.closeTimeout) {
113 this.closeTimeout = setTimeout(function () {
115 }, this.socket.closeTimeout);
120 * Called when transport disconnects.
125 Transport.prototype.onDisconnect = function () {
126 if (this.isOpen) this.close();
127 this.clearTimeouts();
128 this.socket.onDisconnect();
133 * Called when transport connects
138 Transport.prototype.onConnect = function () {
139 this.socket.onConnect();
144 * Clears close timeout
149 Transport.prototype.clearCloseTimeout = function () {
150 if (this.closeTimeout) {
151 clearTimeout(this.closeTimeout);
152 this.closeTimeout = null;
162 Transport.prototype.clearTimeouts = function () {
163 this.clearCloseTimeout();
165 if (this.reopenTimeout) {
166 clearTimeout(this.reopenTimeout);
173 * @param {Object} packet object.
177 Transport.prototype.packet = function (packet) {
178 this.send(io.parser.encodePacket(packet));
182 * Send the received heartbeat message back to server. So the server
183 * knows we are still connected.
185 * @param {String} heartbeat Heartbeat response from the server.
189 Transport.prototype.onHeartbeat = function (heartbeat) {
190 this.packet({ type: 'heartbeat' });
194 * Called when the transport opens.
199 Transport.prototype.onOpen = function () {
201 this.clearCloseTimeout();
202 this.socket.onOpen();
206 * Notifies the base when the connection with the Socket.IO server
207 * has been disconnected.
212 Transport.prototype.onClose = function () {
215 /* FIXME: reopen delay causing a infinit loop
216 this.reopenTimeout = setTimeout(function () {
218 }, this.socket.options['reopen delay']);*/
221 this.socket.onClose();
226 * Generates a connection url based on the Socket.IO URL Protocol.
227 * See <https://github.com/learnboost/socket.io-node/> for more details.
229 * @returns {String} Connection url
233 Transport.prototype.prepareUrl = function () {
234 var options = this.socket.options;
236 return this.scheme() + '://'
237 + options.host + ':' + options.port + '/'
238 + options.resource + '/' + io.protocol
239 + '/' + this.name + '/' + this.sessid;
243 * Checks if the transport is ready to start a connection.
245 * @param {Socket} socket The socket instance that needs a transport
246 * @param {Function} fn The callback
250 Transport.prototype.ready = function (socket, fn) {
254 'undefined' != typeof io ? io : module.exports
255 , 'undefined' != typeof io ? io : module.parent.exports