From 283644b75c49ec1dcd4982e3ce48f1f176e20d1a Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 3 Jul 2015 13:53:40 -0400 Subject: [PATCH 1/2] fix socketio/socket.io-client-swift#110 calling close while trying to reconnect --- SocketIOClientSwift/SocketIOClient.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SocketIOClientSwift/SocketIOClient.swift b/SocketIOClientSwift/SocketIOClient.swift index 7f95fcb..0070e05 100644 --- a/SocketIOClientSwift/SocketIOClient.swift +++ b/SocketIOClientSwift/SocketIOClient.swift @@ -481,7 +481,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient // We lost connection and should attempt to reestablish @objc private func tryReconnect() { - if reconnectAttempts != -1 && currentReconnectAttempt + 1 > reconnectAttempts { + if reconnectAttempts != -1 && currentReconnectAttempt + 1 > reconnectAttempts || !reconnects { didDisconnect("Reconnect Failed") return } else if connected { From 998089df918c504f14bdce95ed806ef4dcf1fdb9 Mon Sep 17 00:00:00 2001 From: Erik Date: Fri, 3 Jul 2015 13:58:06 -0400 Subject: [PATCH 2/2] Invalidate timer on reconnect failure --- SocketIOClientSwift/SocketIOClient.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/SocketIOClientSwift/SocketIOClient.swift b/SocketIOClientSwift/SocketIOClient.swift index 0070e05..26ed4e0 100644 --- a/SocketIOClientSwift/SocketIOClient.swift +++ b/SocketIOClientSwift/SocketIOClient.swift @@ -139,6 +139,11 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient engine = SocketEngine(client: self, opts: opts) } + private func clearReconnectTimer() { + reconnectTimer?.invalidate() + reconnectTimer = nil + } + /** Closes the socket. Only reopen the same socket if you know what you're doing. Will turn off automatic reconnects. @@ -222,8 +227,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient _connecting = false _reconnecting = false currentReconnectAttempt = 0 - reconnectTimer?.invalidate() - reconnectTimer = nil + clearReconnectTimer() // Don't handle as internal because something crazy could happen where // we disconnect before it's handled @@ -482,7 +486,9 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient // We lost connection and should attempt to reestablish @objc private func tryReconnect() { if reconnectAttempts != -1 && currentReconnectAttempt + 1 > reconnectAttempts || !reconnects { + clearReconnectTimer() didDisconnect("Reconnect Failed") + return } else if connected { _connecting = false