2424
2525import Foundation
2626
27- public final class SocketEngine: NSObject , SocketEnginePollable , SocketEngineWebsocket {
27+ public final class SocketEngine : NSObject , SocketEnginePollable , SocketEngineWebsocket {
2828 public let emitQueue = dispatch_queue_create ( " com.socketio.engineEmitQueue " , DISPATCH_QUEUE_SERIAL)
2929 public let handleQueue = dispatch_queue_create ( " com.socketio.engineHandleQueue " , DISPATCH_QUEUE_SERIAL)
3030 public let parseQueue = dispatch_queue_create ( " com.socketio.engineParseQueue " , DISPATCH_QUEUE_SERIAL)
@@ -47,7 +47,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
4747 public private( set) var forcePolling = false
4848 public private( set) var forceWebsockets = false
4949 public private( set) var invalidated = false
50- public private( set) var pingTimer : NSTimer ?
5150 public private( set) var polling = true
5251 public private( set) var probing = false
5352 public private( set) var session : NSURLSession ?
@@ -123,18 +122,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
123122 self . init ( client: client, url: url, options: options? . toSocketOptionsSet ( ) ?? [ ] )
124123 }
125124
126- @available ( * , deprecated= 5.3 , message= " Please use the NSURL based init " )
127- public convenience init ( client: SocketEngineClient , urlString: String , options: Set < SocketIOClientOption > ) {
128- guard let url = NSURL ( string: urlString) else { fatalError ( " Incorrect url " ) }
129- self . init ( client: client, url: url, options: options)
130- }
131-
132- @available ( * , deprecated= 5.3 , message= " Please use the NSURL based init " )
133- public convenience init ( client: SocketEngineClient , urlString: String , options: NSDictionary ? ) {
134- guard let url = NSURL ( string: urlString) else { fatalError ( " Incorrect url " ) }
135- self . init ( client: client, url: url, options: options? . toSocketOptionsSet ( ) ?? [ ] )
136- }
137-
138125 deinit {
139126 DefaultSocketLogger . Logger. log ( " Engine is being released " , type: logType)
140127 closed = true
@@ -184,16 +171,12 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
184171 return false
185172 }
186173 }
187-
188- public func close( reason: String ) {
189- disconnect ( reason)
190- }
191174
192175 /// Starts the connection to the server
193176 public func connect( ) {
194177 if connected {
195178 DefaultSocketLogger . Logger. error ( " Engine tried opening while connected. Assuming this was a reconnect " , type: logType)
196- close ( " reconnect " )
179+ disconnect ( " reconnect " )
197180 }
198181
199182 DefaultSocketLogger . Logger. log ( " Starting engine " , type: logType)
@@ -285,7 +268,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
285268 public func didError( error: String ) {
286269 DefaultSocketLogger . Logger. error ( error, type: logType)
287270 client? . engineDidError ( error)
288- close ( error)
271+ disconnect ( error)
289272 }
290273
291274 public func disconnect( reason: String ) {
@@ -295,7 +278,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
295278 invalidated = true
296279 connected = false
297280
298- pingTimer? . invalidate ( )
299281 ws? . disconnect ( )
300282 stopPolling ( )
301283 client? . engineDidClose ( reason)
@@ -401,8 +383,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
401383 createWebsocketAndConnect ( )
402384 }
403385
404-
405- startPingTimer( )
386+ sendPing( )
406387
407388 if !forceWebsockets {
408389 doPoll ( )
@@ -424,11 +405,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
424405 upgradeTransport ( )
425406 }
426407 }
427-
428- public func open( ) {
429- connect ( )
430- }
431-
408+
432409 public func parseEngineData( data: NSData) {
433410 DefaultSocketLogger . Logger. log ( " Got binary data: %@ " , type: " SocketEngine " , args: data)
434411 client? . parseEngineBinaryData ( data. subdataWithRange ( NSMakeRange ( 1 , data. length - 1 ) ) )
@@ -487,30 +464,28 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
487464 websocket = false
488465 }
489466
490- @objc private func sendPing( ) {
467+ private func sendPing( ) {
468+ if !connected {
469+ return
470+ }
471+
491472 //Server is not responding
492473 if pongsMissed > pongsMissedMax {
493- pingTimer? . invalidate ( )
494474 client? . engineDidClose ( " Ping timeout " )
495475 return
496476 }
497-
498- pongsMissed += 1
499- write ( " " , withType: . Ping, withData: [ ] )
500- }
501-
502- private func startPingTimer( ) {
477+
503478 if let pingInterval = pingInterval {
504- pingTimer ? . invalidate ( )
505- pingTimer = nil
479+ pongsMissed += 1
480+ write ( " " , withType : . Ping , withData : [ ] )
506481
507- dispatch_async ( dispatch_get_main_queue ( ) ) {
508- self . pingTimer = NSTimer . scheduledTimerWithTimeInterval ( pingInterval , target : self ,
509- selector : #selector ( SocketEngine . sendPing) , userInfo : nil , repeats : true )
482+ let time = dispatch_time ( DISPATCH_TIME_NOW , Int64 ( pingInterval * Double ( NSEC_PER_SEC ) ) )
483+ dispatch_after ( time , dispatch_get_main_queue ( ) ) { [ weak self] in
484+ self ? . sendPing ( )
510485 }
511486 }
512487 }
513-
488+
514489 // Moves from long-polling to websockets
515490 private func upgradeTransport( ) {
516491 if ws? . isConnected ?? false {
@@ -562,7 +537,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
562537 }
563538
564539 if websocket {
565- pingTimer? . invalidate ( )
566540 connected = false
567541 websocket = false
568542
0 commit comments