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

Move to DISCONNECTED on unexpected WebSocket close. #656

Merged
merged 1 commit into from
Dec 1, 2017
Merged
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
9 changes: 7 additions & 2 deletions Source/ARTRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -1238,8 +1238,13 @@ - (void)realtimeTransportClosed:(id<ARTRealtimeTransport>)transport {
return;
}

// Close succeeded. Nothing more to do.
[self transition:ARTRealtimeClosed];
if (self.connection.state_nosync == ARTRealtimeClosing) {
// Close succeeded. Nothing more to do.
[self transition:ARTRealtimeClosed];
} else if (self.connection.state_nosync != ARTRealtimeClosed && self.connection.state_nosync != ARTRealtimeFailed) {
// Unexpected closure; recover.
[self transition:ARTRealtimeDisconnected];
}
} ART_TRY_OR_MOVE_TO_FAILED_END
}

Expand Down
5 changes: 1 addition & 4 deletions Source/ARTWebSocketTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,7 @@ - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reas

switch (code) {
case ARTWsCloseNormal:
if (_state == ARTRealtimeTransportStateClosing) {
// OK
[_delegate realtimeTransportClosed:self];
}
[_delegate realtimeTransportClosed:self];
break;
case ARTWsNeverConnected:
[_delegate realtimeTransportNeverConnected:self];
Expand Down
24 changes: 24 additions & 0 deletions Spec/RealtimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,30 @@ class RealtimeClient: QuickSpec {
client.connect()
}
}

fit("moves to DISCONNECTED on an unexpected normal WebSocket close") {
let options = AblyTests.commonAppSetup()
options.disconnectedRetryTimeout = 0.3
let client = ARTRealtime(options: options)
defer { client.dispose(); client.close() }

var received = false
client.channels.get("test").subscribe() { msg in
received = true
}

expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.connected), timeout: testTimeout)

let ws = (client.transport! as! ARTWebSocketTransport).websocket!
ws.close(withCode: 1000, reason: "test")

expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.disconnected), timeout: testTimeout)
expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.connected), timeout: testTimeout)

client.channels.get("test").publish(nil, data: "test")

expect(received).toEventually(beTrue(), timeout: testTimeout)
}
}
}
}