@@ -121,7 +121,8 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
121
121
122
122
self . write ( " " , withType: PacketType . CLOSE, withData: nil )
123
123
self . ws? . disconnect ( )
124
-
124
+ self . stopPolling ( )
125
+
125
126
if fast || self . polling {
126
127
self . client? . engineDidClose ( " Disconnect " )
127
128
}
@@ -252,7 +253,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
252
253
SocketLogger . log ( " Got polling response " , client: self !)
253
254
254
255
if let str = NSString ( data: data, encoding: NSUTF8StringEncoding) as? String {
255
- dispatch_async ( self !. parseQueue) {
256
+ dispatch_async ( self !. parseQueue) { [ weak self ] in
256
257
self ? . parsePollingMessage ( str)
257
258
}
258
259
}
@@ -337,7 +338,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
337
338
}
338
339
339
340
self ? . waitingForPost = false
340
- dispatch_async ( self !. emitQueue) {
341
+ dispatch_async ( self !. emitQueue) { [ weak self ] in
341
342
if !self !. fastUpgrade {
342
343
self ? . flushWaitingForPost ( )
343
344
self ? . doPoll ( )
@@ -363,7 +364,8 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
363
364
self . waitingForPoll = false
364
365
self . waitingForPost = false
365
366
366
- if self . client == nil {
367
+ // If cancelled we were already closing
368
+ if self . client == nil || reason == " cancelled " {
367
369
return
368
370
}
369
371
@@ -445,7 +447,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
445
447
446
448
if let lengthInt = length. toInt ( ) {
447
449
if lengthInt != msg. length {
448
- NSLog ( " parsing error: \( str) " )
450
+ SocketLogger . err ( " parsing error: \( str) " , client : self )
449
451
return
450
452
}
451
453
}
@@ -630,13 +632,21 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
630
632
}
631
633
632
634
self . pingTimer? . invalidate ( )
633
- dispatch_async ( dispatch_get_main_queue ( ) ) {
634
- self . pingTimer = NSTimer . scheduledTimerWithTimeInterval ( NSTimeInterval ( self . pingInterval!) ,
635
- target: self ,
635
+ dispatch_async ( dispatch_get_main_queue ( ) ) { [ weak self] in
636
+ if self == nil {
637
+ return
638
+ }
639
+
640
+ self ? . pingTimer = NSTimer . scheduledTimerWithTimeInterval ( NSTimeInterval ( self !. pingInterval!) ,
641
+ target: self !,
636
642
selector: Selector ( " sendPing " ) , userInfo: nil , repeats: true )
637
643
}
638
644
}
639
645
646
+ func stopPolling( ) {
647
+ self . session. invalidateAndCancel ( )
648
+ }
649
+
640
650
private func upgradeTransport( ) {
641
651
if self . websocketConnected {
642
652
SocketLogger . log ( " Upgrading transport to WebSockets " , client: self )
0 commit comments