From 6bc02697f597f4862fb8808435546ae76c1bdb7f Mon Sep 17 00:00:00 2001 From: Allen Lin Date: Fri, 28 Jun 2019 17:31:49 +0800 Subject: [PATCH 1/3] fix: handle failure in connect --- Source/SocketIO/Client/SocketIOClient.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Source/SocketIO/Client/SocketIOClient.swift b/Source/SocketIO/Client/SocketIOClient.swift index 355fd210..5d1e073d 100644 --- a/Source/SocketIO/Client/SocketIOClient.swift +++ b/Source/SocketIO/Client/SocketIOClient.swift @@ -135,6 +135,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec { guard let manager = self.manager, status != .connected else { DefaultSocketLogger.Logger.log("Tried connecting on an already connected socket", type: logType) + handler?() return } @@ -152,8 +153,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec { guard timeoutAfter != 0 else { return } manager.handleQueue.asyncAfter(deadline: DispatchTime.now() + timeoutAfter) {[weak self] in - guard let this = self, this.status == .connecting || this.status == .notConnected else { return } - + guard let this = self, this.status == .connecting || this.status == .notConnected else { + handler?() + return + } + this.status = .disconnected this.leaveNamespace() From a843176ee76e3ca988c4f6f6b5769bfb4e362afb Mon Sep 17 00:00:00 2001 From: allenlinli Date: Sun, 22 Sep 2019 21:24:42 +0800 Subject: [PATCH 2/3] feat: add test `testConnectedAlready` --- Tests/TestSocketIO/SocketSideEffectTest.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Tests/TestSocketIO/SocketSideEffectTest.swift b/Tests/TestSocketIO/SocketSideEffectTest.swift index a37f198a..07984014 100644 --- a/Tests/TestSocketIO/SocketSideEffectTest.swift +++ b/Tests/TestSocketIO/SocketSideEffectTest.swift @@ -355,6 +355,19 @@ class SocketSideEffectTest: XCTestCase { waitForExpectations(timeout: 2) } + func testConnectedAlready() { + let expect = expectation(description: "The client should call its handler if it's connected already") + + socket.setTestStatus(.connected) + manager.engine = TestEngine(client: manager, url: manager.socketURL, options: nil) + + socket.connect(timeoutAfter: 0.5, withHandler: { + expect.fulfill() + }) + + waitForExpectations(timeout: 0.8) + } + func testErrorInCustomSocketDataCallsErrorHandler() { let expect = expectation(description: "The client should call the error handler for emit errors because of " + "custom data") From f2c892b993720ec26650eec22afe5d208ec18069 Mon Sep 17 00:00:00 2001 From: allenlinli Date: Thu, 2 Jan 2020 18:00:11 +0800 Subject: [PATCH 3/3] fix: fix the exception that it is connected already when it connects --- Source/SocketIO/Client/SocketIOClient.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Source/SocketIO/Client/SocketIOClient.swift b/Source/SocketIO/Client/SocketIOClient.swift index 5d1e073d..ce237eb6 100644 --- a/Source/SocketIO/Client/SocketIOClient.swift +++ b/Source/SocketIO/Client/SocketIOClient.swift @@ -154,7 +154,10 @@ open class SocketIOClient : NSObject, SocketIOClientSpec { manager.handleQueue.asyncAfter(deadline: DispatchTime.now() + timeoutAfter) {[weak self] in guard let this = self, this.status == .connecting || this.status == .notConnected else { - handler?() + guard self?.status == .connected else { + handler?() + return + } return }