Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit f18b66b

Browse files
committed
Merge commit '5b4347661a29d1b191e21dcce4564af533613155' into develop
2 parents cde0458 + 5b43476 commit f18b66b

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

SocketIO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ typedef enum {
7979

8080
// heartbeat
8181
NSTimeInterval _heartbeatTimeout;
82-
NSTimer *_timeout;
82+
dispatch_source_t _timeout;
8383

8484
NSMutableArray *_queue;
8585

SocketIO.m

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@ - (void) removeAcknowledgeForKey:(NSString *)key
398398

399399
- (void) onTimeout
400400
{
401+
if (_timeout) {
402+
dispatch_source_cancel(_timeout);
403+
_timeout = NULL;
404+
}
405+
401406
DEBUGLOG(@"Timed out waiting for heartbeat.");
402407
[self onDisconnect:[NSError errorWithDomain:SocketIOError
403408
code:SocketIOHeartbeatTimeout
@@ -407,16 +412,29 @@ - (void) onTimeout
407412
- (void) setTimeout
408413
{
409414
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;
413418
}
414419

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+
420438
}
421439

422440

@@ -595,9 +613,9 @@ - (void) onDisconnect:(NSError *)error
595613
[_queue removeAllObjects];
596614

597615
// 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;
601619
}
602620

603621
// Disconnect the websocket, just in case
@@ -811,8 +829,10 @@ - (void) dealloc
811829
_transport.delegate = nil;
812830
_transport = nil;
813831

814-
[_timeout invalidate];
815-
_timeout = nil;
832+
if (_timeout) {
833+
dispatch_source_cancel(_timeout);
834+
_timeout = NULL;
835+
}
816836

817837
_queue = nil;
818838
_acks = nil;

0 commit comments

Comments
 (0)