Skip to content

Commit 04f1e7d

Browse files
author
Daniel Rees
committed
Updated transport to call onClose after onError
1 parent 7f11476 commit 04f1e7d

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

src/main/kotlin/org/phoenixframework/Socket.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ const val WS_CLOSE_NORMAL = 1000
5454
/** RFC 6455: indicates that the connection was closed abnormally */
5555
const val WS_CLOSE_ABNORMAL = 1006
5656

57-
/** The socket was closed due to a SocketException. Likely the client lost connectivity */
58-
// DEPRECATED
59-
const val WS_CLOSE_SOCKET_EXCEPTION = 4000
6057

6158
/**
6259
* Connects to a Phoenix Server
@@ -414,8 +411,14 @@ class Socket(
414411
ref = pendingHeartbeatRef)
415412
}
416413

417-
internal fun abnormalClose(reason: String) {
414+
private fun abnormalClose(reason: String) {
418415
this.closeWasClean = false
416+
417+
/*
418+
We use NORMAL here since the client is the one determining to close the connection. However,
419+
we keep a flag `closeWasClean` set to false so that the client knows that it should attempt
420+
to reconnect.
421+
*/
419422
this.connection?.disconnect(WS_CLOSE_NORMAL, reason)
420423
}
421424

src/main/kotlin/org/phoenixframework/Transport.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import okhttp3.Request
2727
import okhttp3.Response
2828
import okhttp3.WebSocket
2929
import okhttp3.WebSocketListener
30-
import java.io.IOException
3130
import java.net.URL
3231

3332
/**
@@ -130,13 +129,28 @@ class WebSocketTransport(
130129
}
131130

132131
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
132+
// Set the state of the Transport as CLOSED since no more data will be received
133133
this.readyState = Transport.ReadyState.CLOSED
134+
135+
// Invoke the onError callback, to inform of the error
134136
this.onError?.invoke(t, response)
135-
136-
// Check if the socket was closed for some recoverable reason
137-
when (t) {
138-
is IOException -> this.onClosed(webSocket, WS_CLOSE_SOCKET_EXCEPTION, "IOException")
139-
}
137+
138+
/*
139+
According to the OkHttp documentation, `onFailure` will be
140+
141+
"Invoked when a web socket has been closed due to an error reading from or writing to the
142+
network. Both outgoing and incoming messages may have been lost. No further calls to this
143+
listener will be made."
144+
145+
This means `onClose` will never be called which will never kick off the socket reconnect
146+
attempts.
147+
148+
The JS WebSocket class calls `onError` and then `onClose` which will then trigger
149+
the reconnect logic inside of the PhoenixClient. In order to mimic this behavior and abstract
150+
this detail of OkHttp away from the PhoenixClient, the `WebSocketTransport` class should
151+
convert `onFailure` calls to an `onError` and `onClose` sequence.
152+
*/
153+
this.onClose?.invoke(WS_CLOSE_ABNORMAL)
140154
}
141155

142156
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {

src/test/kotlin/org/phoenixframework/SocketTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ class SocketTest {
294294
verify(connection).disconnect(WS_CLOSE_NORMAL)
295295
}
296296

297+
@Test
298+
internal fun `flags the socket as closed cleanly`() {
299+
assertThat(socket.closeWasClean).isFalse()
300+
301+
socket.disconnect()
302+
assertThat(socket.closeWasClean).isTrue()
303+
}
304+
297305
@Test
298306
internal fun `calls callback`() {
299307
val mockCallback = mock<() -> Unit> {}

src/test/kotlin/org/phoenixframework/WebSocketTransportTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import org.junit.jupiter.api.Nested
1414
import org.junit.jupiter.api.Test
1515
import org.mockito.Mock
1616
import org.mockito.MockitoAnnotations
17-
import org.phoenixframework.Transport
18-
import org.phoenixframework.WebSocketTransport
1917
import java.net.SocketException
2018
import java.net.URL
2119

@@ -124,7 +122,7 @@ class WebSocketTransportTest {
124122
val throwable = SocketException()
125123
transport.onFailure(mockWebSocket, throwable, mockResponse)
126124
verify(mockOnError).invoke(throwable, mockResponse)
127-
verify(mockOnClose).invoke(4000)
125+
verify(mockOnClose).invoke(1006)
128126
}
129127

130128
/* End OnFailure */

0 commit comments

Comments
 (0)