Skip to content

Commit

Permalink
Move to DISCONNECTED on unexpected WebSocket close. (#656)
Browse files Browse the repository at this point in the history
Should fix #655.
  • Loading branch information
tcard authored Dec 1, 2017
1 parent a31ba9f commit f5fc053
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
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)
}
}
}
}

0 comments on commit f5fc053

Please sign in to comment.