diff --git a/SocketIO.m b/SocketIO.m index 61ff67a..3d0d077 100755 --- a/SocketIO.m +++ b/SocketIO.m @@ -404,11 +404,8 @@ - (void) removeAcknowledgeForKey:(NSString *)key - (void) onTimeout { - if (_timeout) { - dispatch_source_cancel(_timeout); - _timeout = NULL; - } - + [ self cleanupHeartbeatTimeout ]; + DEBUGLOG(@"Timed out waiting for heartbeat."); [self onDisconnect:[NSError errorWithDomain:SocketIOError code:SocketIOHeartbeatTimeout @@ -418,11 +415,8 @@ - (void) onTimeout - (void) setTimeout { DEBUGLOG(@"start/reset timeout"); - if (_timeout) { - dispatch_source_cancel(_timeout); - _timeout = NULL; - } - + [ self cleanupHeartbeatTimeout ]; + _timeout = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, @@ -619,11 +613,8 @@ - (void) onDisconnect:(NSError *)error [_queue removeAllObjects]; // Kill the heartbeat timer - if (_timeout) { - dispatch_source_cancel(_timeout); - _timeout = NULL; - } - + [ self cleanupHeartbeatTimeout ]; + // Disconnect the websocket, just in case if (_transport != nil) { // clear websocket's delegate - otherwise crashes @@ -819,6 +810,15 @@ - (void) connection:(NSURLConnection *)connection } #endif +# pragma mark - Cleanup Heartbeat Timer +-(void) cleanupHeartbeatTimeout +{ + if (_timeout) { + dispatch_source_cancel(_timeout); + dispatch_release(_timeout); + _timeout = NULL; + } +} # pragma mark - @@ -834,11 +834,8 @@ - (void) dealloc _transport.delegate = nil; _transport = nil; - if (_timeout) { - dispatch_source_cancel(_timeout); - _timeout = NULL; - } - + [ self cleanupHeartbeatTimeout ]; + _queue = nil; _acks = nil; }