UNPKG

4.78 kBJavaScriptView Raw
1
2/**
3 * socket.io
4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5 * MIT Licensed
6 */
7
8(function (exports, io) {
9
10 /**
11 * Expose constructor.
12 */
13
14 exports.Transport = Transport;
15
16 /**
17 * This is the transport template for all supported transport methods.
18 *
19 * @constructor
20 * @api public
21 */
22
23 function Transport (socket, sessid) {
24 this.socket = socket;
25 this.sessid = sessid;
26 };
27
28 /**
29 * Apply EventEmitter mixin.
30 */
31
32 io.util.mixin(Transport, io.EventEmitter);
33
34 /**
35 * Handles the response from the server. When a new response is received
36 * it will automatically update the timeout, decode the message and
37 * forwards the response to the onMessage function for further processing.
38 *
39 * @param {String} data Response from the server.
40 * @api private
41 */
42
43 Transport.prototype.onData = function (data) {
44 this.clearCloseTimeout();
45 this.setCloseTimeout();
46
47 if (data !== '') {
48 // todo: we should only do decodePayload for xhr transports
49 var msgs = io.parser.decodePayload(data);
50
51 if (msgs && msgs.length) {
52 for (var i = 0, l = msgs.length; i < l; i++) {
53 this.onPacket(msgs[i]);
54 }
55 }
56 }
57
58 return this;
59 };
60
61 /**
62 * Handles packets.
63 *
64 * @api private
65 */
66
67 Transport.prototype.onPacket = function (packet) {
68 if (packet.type == 'heartbeat') {
69 return this.onHeartbeat();
70 }
71
72 if (packet.type == 'connect' && packet.endpoint == '') {
73 this.onConnect();
74 }
75
76 this.socket.onPacket(packet);
77
78 return this;
79 };
80
81 /**
82 * Sets close timeout
83 *
84 * @api private
85 */
86
87 Transport.prototype.setCloseTimeout = function () {
88 if (!this.closeTimeout) {
89 var self = this;
90
91 this.closeTimeout = setTimeout(function () {
92 self.onDisconnect();
93 }, this.socket.closeTimeout);
94 }
95 };
96
97 /**
98 * Called when transport disconnects.
99 *
100 * @api private
101 */
102
103 Transport.prototype.onDisconnect = function () {
104 if (this.close) this.close();
105 this.clearTimeouts();
106 this.socket.onDisconnect();
107 return this;
108 };
109
110 /**
111 * Called when transport connects
112 *
113 * @api private
114 */
115
116 Transport.prototype.onConnect = function () {
117 this.socket.onConnect();
118 return this;
119 }
120
121 /**
122 * Clears close timeout
123 *
124 * @api private
125 */
126
127 Transport.prototype.clearCloseTimeout = function () {
128 if (this.closeTimeout) {
129 clearTimeout(this.closeTimeout);
130 this.closeTimeout = null;
131 }
132 };
133
134 /**
135 * Clear timeouts
136 *
137 * @api private
138 */
139
140 Transport.prototype.clearTimeouts = function () {
141 this.clearCloseTimeout();
142
143 if (this.reopenTimeout) {
144 clearTimeout(this.reopenTimeout);
145 }
146 };
147
148 /**
149 * Sends a packet
150 *
151 * @param {Object} packet object.
152 * @api private
153 */
154
155 Transport.prototype.packet = function (packet) {
156 this.send(io.parser.encodePacket(packet));
157 };
158
159 /**
160 * Send the received heartbeat message back to server. So the server
161 * knows we are still connected.
162 *
163 * @param {String} heartbeat Heartbeat response from the server.
164 * @api private
165 */
166
167 Transport.prototype.onHeartbeat = function (heartbeat) {
168 this.packet({ type: 'heartbeat' });
169 };
170
171 /**
172 * Called when the transport opens.
173 *
174 * @api private
175 */
176
177 Transport.prototype.onOpen = function () {
178 this.open = true;
179 this.clearCloseTimeout();
180 this.socket.onOpen();
181 };
182
183 /**
184 * Notifies the base when the connection with the Socket.IO server
185 * has been disconnected.
186 *
187 * @api private
188 */
189
190 Transport.prototype.onClose = function () {
191 var self = this;
192
193 /* FIXME: reopen delay causing a infinit loop
194 this.reopenTimeout = setTimeout(function () {
195 self.open();
196 }, this.socket.options['reopen delay']);*/
197
198 this.open = false;
199 this.setCloseTimeout();
200 this.socket.onClose();
201 };
202
203 /**
204 * Generates a connection url based on the Socket.IO URL Protocol.
205 * See <https://github.com/learnboost/socket.io-node/> for more details.
206 *
207 * @returns {String} Connection url
208 * @api private
209 */
210
211 Transport.prototype.prepareUrl = function () {
212 var options = this.socket.options;
213
214 return this.scheme() + '://'
215 + options.host + ':' + options.port + '/'
216 + options.resource + '/' + io.protocol
217 + '/' + this.name + '/' + this.sessid;
218 };
219
220 /**
221 * Checks if the transport is ready to start a connection.
222 *
223 * @param {Socket} socket The socket instance that needs a transport
224 * @param {Function} fn The callback
225 * @api private
226 */
227
228 Transport.prototype.ready = function (socket, fn) {
229 fn.call(this);
230 };
231})(
232 'undefined' != typeof io ? io : module.exports
233 , 'undefined' != typeof io ? io : module.parent.exports
234);