diff --git a/Source/SocketIO/Client/SocketIOClient.swift b/Source/SocketIO/Client/SocketIOClient.swift index 355fd210..ce237eb6 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,14 @@ 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 { + guard self?.status == .connected else { + handler?() + return + } + return + } + this.status = .disconnected this.leaveNamespace() 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")