Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix connection error handling if the connection state is wrong #1233

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Source/SocketIO/Client/SocketIOClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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()

Expand Down
13 changes: 13 additions & 0 deletions Tests/TestSocketIO/SocketSideEffectTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down