@@ -41,6 +41,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate {
4141 private var fastUpgrade = false
4242 private var forcePolling = false
4343 private var forceWebsockets = false
44+ private var invalidated = false
4445 private var pingInterval : Double ?
4546 private var pingTimer : NSTimer ?
4647 private var pingTimeout = 0.0 {
@@ -99,6 +100,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate {
99100
100101 deinit {
101102 Logger . log ( " Engine is being deinit " , type: logType)
103+ closed = true
102104 stopPolling ( )
103105 }
104106
@@ -248,19 +250,23 @@ public final class SocketEngine: NSObject, WebSocketDelegate {
248250 }
249251 }
250252
251- doRequest ( req)
253+ doLongPoll ( req)
252254 }
255+
256+ private func doRequest( req: NSMutableURLRequest ,
257+ withCallback callback: ( NSData ? , NSURLResponse ? , NSError ? ) -> Void ) {
258+ if !polling || closed || invalidated {
259+ return
260+ }
261+
262+ Logger . log ( " Doing polling request " , type: logType)
253263
254- private func doRequest( req: NSMutableURLRequest ) {
255- if !polling || closed {
256- return
257- }
258-
259- req. cachePolicy = NSURLRequestCachePolicy . ReloadIgnoringLocalAndRemoteCacheData
260-
261- Logger . log ( " Doing polling request " , type: logType)
264+ req. cachePolicy = NSURLRequestCachePolicy . ReloadIgnoringLocalAndRemoteCacheData
265+ session. dataTaskWithRequest ( req, completionHandler: callback) . resume ( )
266+ }
262267
263- session. dataTaskWithRequest ( req) { [ weak self] data, res, err in
268+ private func doLongPoll( req: NSMutableURLRequest ) {
269+ doRequest ( req) { [ weak self] data, res, err in
264270 if let this = self {
265271 if err != nil || data == nil {
266272 if this. polling {
@@ -270,23 +276,24 @@ public final class SocketEngine: NSObject, WebSocketDelegate {
270276 }
271277 return
272278 }
273-
279+
274280 Logger . log ( " Got polling response " , type: this. logType)
275-
281+
276282 if let str = NSString ( data: data!, encoding: NSUTF8StringEncoding) as? String {
277283 dispatch_async ( this. parseQueue) { [ weak this] in
278284 this? . parsePollingMessage ( str)
279285 }
280286 }
281-
287+
282288 this. waitingForPoll = false
283-
289+
284290 if this. fastUpgrade {
285291 this. doFastUpgrade ( )
286292 } else if !this. closed && this. polling {
287293 this. doPoll ( )
288294 }
289- } } . resume ( )
295+ }
296+ }
290297 }
291298
292299 private func flushProbeWait( ) {
@@ -345,13 +352,13 @@ public final class SocketEngine: NSObject, WebSocketDelegate {
345352
346353 Logger . log ( " POSTing: %@ " , type: logType, args: postStr)
347354
348- session . dataTaskWithRequest ( req) { [ weak self] data, res, err in
355+ doRequest ( req) { [ weak self] data, res, err in
349356 if let this = self {
350357 if err != nil && this. polling {
351358 this. handlePollingFailed ( err? . localizedDescription ?? " Error " )
352359 return
353360 } else if err != nil {
354- NSLog ( err? . localizedDescription ?? " Error " )
361+ Logger . error ( err? . localizedDescription ?? " Error " , type : this . logType )
355362 return
356363 }
357364
@@ -363,7 +370,8 @@ public final class SocketEngine: NSObject, WebSocketDelegate {
363370 this? . doPoll ( )
364371 }
365372 }
366- } } . resume ( )
373+ }
374+ }
367375 }
368376
369377 // We had packets waiting for send when we upgraded
@@ -488,7 +496,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate {
488496 }
489497 }
490498
491- doRequest ( reqPolling)
499+ doLongPoll ( reqPolling)
492500 }
493501
494502 private func parsePollingMessage( str: String) {
@@ -627,6 +635,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate {
627635 }
628636
629637 func stopPolling( ) {
638+ invalidated = true
630639 session. finishTasksAndInvalidate ( )
631640 }
632641
0 commit comments