@@ -359,6 +359,11 @@ - (void) removeAcknowledgeForKey:(NSString *)key
359
359
360
360
- (void ) onTimeout
361
361
{
362
+ if (_timeout) {
363
+ dispatch_source_cancel (_timeout);
364
+ _timeout = NULL ;
365
+ }
366
+
362
367
DEBUGLOG (@" Timed out waiting for heartbeat." );
363
368
[self onDisconnect: [NSError errorWithDomain: SocketIOError
364
369
code: SocketIOHeartbeatTimeout
@@ -368,16 +373,29 @@ - (void) onTimeout
368
373
- (void ) setTimeout
369
374
{
370
375
DEBUGLOG (@" start/reset timeout" );
371
- if (_timeout != nil ) {
372
- [ _timeout invalidate ] ;
373
- _timeout = nil ;
376
+ if (_timeout) {
377
+ dispatch_source_cancel ( _timeout) ;
378
+ _timeout = NULL ;
374
379
}
375
380
376
- _timeout = [NSTimer scheduledTimerWithTimeInterval: _heartbeatTimeout
377
- target: self
378
- selector: @selector (onTimeout )
379
- userInfo: nil
380
- repeats: NO ];
381
+ _timeout = dispatch_source_create (DISPATCH_SOURCE_TYPE_TIMER,
382
+ 0 ,
383
+ 0 ,
384
+ dispatch_get_main_queue ());
385
+
386
+ dispatch_source_set_timer (_timeout,
387
+ dispatch_time (DISPATCH_TIME_NOW, _heartbeatTimeout * NSEC_PER_SEC),
388
+ 0 ,
389
+ 0 );
390
+
391
+ __weak SocketIO *weakSelf = self;
392
+
393
+ dispatch_source_set_event_handler (_timeout, ^{
394
+ [weakSelf onTimeout ];
395
+ });
396
+
397
+ dispatch_resume (_timeout);
398
+
381
399
}
382
400
383
401
@@ -556,9 +574,9 @@ - (void) onDisconnect:(NSError *)error
556
574
[_queue removeAllObjects ];
557
575
558
576
// Kill the heartbeat timer
559
- if (_timeout != nil ) {
560
- [ _timeout invalidate ] ;
561
- _timeout = nil ;
577
+ if (_timeout) {
578
+ dispatch_source_cancel ( _timeout) ;
579
+ _timeout = NULL ;
562
580
}
563
581
564
582
// Disconnect the websocket, just in case
@@ -768,8 +786,10 @@ - (void) dealloc
768
786
769
787
_transport = nil ;
770
788
771
- [_timeout invalidate ];
772
- _timeout = nil ;
789
+ if (_timeout) {
790
+ dispatch_source_cancel (_timeout);
791
+ _timeout = NULL ;
792
+ }
773
793
774
794
_queue = nil ;
775
795
_acks = nil ;
0 commit comments