@@ -26,6 +26,7 @@ function Socket (id, server, transport, req) {
26
26
this . writeBuffer = [ ] ;
27
27
this . packetsFn = [ ] ;
28
28
this . sentCallbackFn = [ ] ;
29
+ this . cleanupFn = [ ] ;
29
30
this . request = req ;
30
31
31
32
// Cache IP since it might not be in the req later
@@ -93,7 +94,6 @@ Socket.prototype.onPacket = function (packet) {
93
94
break ;
94
95
95
96
case 'error' :
96
- this . transport . close ( ) ;
97
97
this . onClose ( 'parse error' ) ;
98
98
break ;
99
99
@@ -129,7 +129,6 @@ Socket.prototype.setPingTimeout = function () {
129
129
var self = this ;
130
130
clearTimeout ( self . pingTimeoutTimer ) ;
131
131
self . pingTimeoutTimer = setTimeout ( function ( ) {
132
- self . transport . close ( ) ;
133
132
self . onClose ( 'ping timeout' ) ;
134
133
} , self . server . pingInterval + self . server . pingTimeout ) ;
135
134
} ;
@@ -142,13 +141,25 @@ Socket.prototype.setPingTimeout = function () {
142
141
*/
143
142
144
143
Socket . prototype . setTransport = function ( transport ) {
144
+ var onError = this . onError . bind ( this ) ;
145
+ var onPacket = this . onPacket . bind ( this ) ;
146
+ var flush = this . flush . bind ( this ) ;
147
+ var onClose = this . onClose . bind ( this , 'transport close' ) ;
148
+
145
149
this . transport = transport ;
146
- this . transport . once ( 'error' , this . onError . bind ( this ) ) ;
147
- this . transport . on ( 'packet' , this . onPacket . bind ( this ) ) ;
148
- this . transport . on ( 'drain' , this . flush . bind ( this ) ) ;
149
- this . transport . once ( 'close' , this . onClose . bind ( this , 'transport close' ) ) ;
150
+ this . transport . once ( 'error' , onError ) ;
151
+ this . transport . on ( 'packet' , onPacket ) ;
152
+ this . transport . on ( 'drain' , flush ) ;
153
+ this . transport . once ( 'close' , onClose ) ;
150
154
//this function will manage packet events (also message callbacks)
151
155
this . setupSendCallback ( ) ;
156
+
157
+ this . cleanupFn . push ( function ( ) {
158
+ transport . removeListener ( 'error' , onError ) ;
159
+ transport . removeListener ( 'packet' , onPacket ) ;
160
+ transport . removeListener ( 'drain' , flush ) ;
161
+ transport . removeListener ( 'close' , onClose ) ;
162
+ } ) ;
152
163
} ;
153
164
154
165
/**
@@ -178,6 +189,7 @@ Socket.prototype.maybeUpgrade = function (transport) {
178
189
function onPacket ( packet ) {
179
190
if ( 'ping' == packet . type && 'probe' == packet . data ) {
180
191
transport . send ( [ { type : 'pong' , data : 'probe' , options : { compress : true } } ] ) ;
192
+ self . emit ( 'upgrading' , transport ) ;
181
193
clearInterval ( self . checkIntervalTimer ) ;
182
194
self . checkIntervalTimer = setInterval ( check , 100 ) ;
183
195
} else if ( 'upgrade' == packet . type && self . readyState != 'closed' ) {
@@ -252,6 +264,9 @@ Socket.prototype.maybeUpgrade = function (transport) {
252
264
*/
253
265
254
266
Socket . prototype . clearTransport = function ( ) {
267
+ var cleanup ;
268
+ while ( cleanup = this . cleanupFn . shift ( ) ) cleanup ( ) ;
269
+
255
270
// silence further transport errors and prevent uncaught exceptions
256
271
this . transport . on ( 'error' , function ( ) {
257
272
debug ( 'error triggered by discarded transport' ) ;
@@ -271,6 +286,7 @@ Socket.prototype.clearTransport = function () {
271
286
272
287
Socket . prototype . onClose = function ( reason , description ) {
273
288
if ( 'closed' != this . readyState ) {
289
+ this . readyState = 'closed' ;
274
290
clearTimeout ( this . pingTimeoutTimer ) ;
275
291
clearInterval ( this . checkIntervalTimer ) ;
276
292
this . checkIntervalTimer = null ;
@@ -284,7 +300,6 @@ Socket.prototype.onClose = function (reason, description) {
284
300
this . packetsFn = [ ] ;
285
301
this . sentCallbackFn = [ ] ;
286
302
this . clearTransport ( ) ;
287
- this . readyState = 'closed' ;
288
303
this . emit ( 'close' , reason , description ) ;
289
304
}
290
305
} ;
@@ -297,8 +312,14 @@ Socket.prototype.onClose = function (reason, description) {
297
312
298
313
Socket . prototype . setupSendCallback = function ( ) {
299
314
var self = this ;
315
+ this . transport . on ( 'drain' , onDrain ) ;
316
+
317
+ this . cleanupFn . push ( function ( ) {
318
+ self . transport . removeListener ( 'drain' , onDrain ) ;
319
+ } ) ;
320
+
300
321
//the message was sent successfully, execute the callback
301
- this . transport . on ( 'drain' , function ( ) {
322
+ function onDrain ( ) {
302
323
if ( self . sentCallbackFn . length > 0 ) {
303
324
var seqFn = self . sentCallbackFn . splice ( 0 , 1 ) [ 0 ] ;
304
325
if ( 'function' == typeof seqFn ) {
@@ -313,7 +334,7 @@ Socket.prototype.setupSendCallback = function () {
313
334
}
314
335
}
315
336
}
316
- } ) ;
337
+ }
317
338
} ;
318
339
319
340
/**
0 commit comments