@@ -398,6 +398,11 @@ - (void) removeAcknowledgeForKey:(NSString *)key
398
398
399
399
- (void ) onTimeout
400
400
{
401
+ if (_timeout) {
402
+ dispatch_source_cancel (_timeout);
403
+ _timeout = NULL ;
404
+ }
405
+
401
406
DEBUGLOG (@" Timed out waiting for heartbeat." );
402
407
[self onDisconnect: [NSError errorWithDomain: SocketIOError
403
408
code: SocketIOHeartbeatTimeout
@@ -407,16 +412,29 @@ - (void) onTimeout
407
412
- (void ) setTimeout
408
413
{
409
414
DEBUGLOG (@" start/reset timeout" );
410
- if (_timeout != nil ) {
411
- [ _timeout invalidate ] ;
412
- _timeout = nil ;
415
+ if (_timeout) {
416
+ dispatch_source_cancel ( _timeout) ;
417
+ _timeout = NULL ;
413
418
}
414
419
415
- _timeout = [NSTimer scheduledTimerWithTimeInterval: _heartbeatTimeout
416
- target: self
417
- selector: @selector (onTimeout )
418
- userInfo: nil
419
- repeats: NO ];
420
+ _timeout = dispatch_source_create (DISPATCH_SOURCE_TYPE_TIMER,
421
+ 0 ,
422
+ 0 ,
423
+ dispatch_get_main_queue ());
424
+
425
+ dispatch_source_set_timer (_timeout,
426
+ dispatch_time (DISPATCH_TIME_NOW, _heartbeatTimeout * NSEC_PER_SEC),
427
+ 0 ,
428
+ 0 );
429
+
430
+ __weak SocketIO *weakSelf = self;
431
+
432
+ dispatch_source_set_event_handler (_timeout, ^{
433
+ [weakSelf onTimeout ];
434
+ });
435
+
436
+ dispatch_resume (_timeout);
437
+
420
438
}
421
439
422
440
@@ -595,9 +613,9 @@ - (void) onDisconnect:(NSError *)error
595
613
[_queue removeAllObjects ];
596
614
597
615
// Kill the heartbeat timer
598
- if (_timeout != nil ) {
599
- [ _timeout invalidate ] ;
600
- _timeout = nil ;
616
+ if (_timeout) {
617
+ dispatch_source_cancel ( _timeout) ;
618
+ _timeout = NULL ;
601
619
}
602
620
603
621
// Disconnect the websocket, just in case
@@ -811,8 +829,10 @@ - (void) dealloc
811
829
_transport.delegate = nil ;
812
830
_transport = nil ;
813
831
814
- [_timeout invalidate ];
815
- _timeout = nil ;
832
+ if (_timeout) {
833
+ dispatch_source_cancel (_timeout);
834
+ _timeout = NULL ;
835
+ }
816
836
817
837
_queue = nil ;
818
838
_acks = nil ;
0 commit comments