From 44b32bb6995834c3f674ccc98d7f2afa418af3c8 Mon Sep 17 00:00:00 2001 From: Daniel Rees Date: Mon, 11 Feb 2019 08:42:13 -0500 Subject: [PATCH 1/2] Do not schedule reconnect after a normal closure --- src/main/kotlin/org/phoenixframework/PhxSocket.kt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/org/phoenixframework/PhxSocket.kt b/src/main/kotlin/org/phoenixframework/PhxSocket.kt index ef0e2ff..c8f1125 100644 --- a/src/main/kotlin/org/phoenixframework/PhxSocket.kt +++ b/src/main/kotlin/org/phoenixframework/PhxSocket.kt @@ -23,6 +23,9 @@ const val DEFAULT_TIMEOUT: Long = 10000 /** Default heartbeat interval set to 30s */ const val DEFAULT_HEARTBEAT: Long = 30000 +/** The code used when the socket was closed without error */ +const val WS_CLOSE_NORMAL = 1000 + open class PhxSocket( url: String, params: Payload? = null, @@ -155,7 +158,7 @@ open class PhxSocket( * Disconnects the Socket */ fun disconnect() { - connection?.close(1000, null) + connection?.close(WS_CLOSE_NORMAL, null) connection = null } @@ -337,15 +340,16 @@ open class PhxSocket( } /** Triggers a message when the socket is closed */ - private fun onConnectionClosed() { + private fun onConnectionClosed(code: Int) { this.logItems("Transport: close") this.triggerChannelError() // Terminate any ongoing heartbeats this.heartbeatTimer?.cancel() - // Attempt to reconnect the socket - if (autoReconnect) reconnectTimer?.scheduleTimeout() + // Attempt to reconnect the socket. If the socket was closed normally, + // then do not attempt to reconnect + if (autoReconnect && code != WS_CLOSE_NORMAL) reconnectTimer?.scheduleTimeout() // Inform all onClose callbacks that the Socket closed this.onCloseCallbacks.forEach { it() } @@ -444,7 +448,7 @@ open class PhxSocket( } override fun onClosed(webSocket: WebSocket?, code: Int, reason: String?) { - this.onConnectionClosed() + this.onConnectionClosed(code) } override fun onFailure(webSocket: WebSocket?, t: Throwable, response: Response?) { From ca173f3d92a2df0b2cb86e0f5c75783f4f0e3ef4 Mon Sep 17 00:00:00 2001 From: Daniel Rees Date: Mon, 11 Feb 2019 20:43:02 -0500 Subject: [PATCH 2/2] When disconneting the socket due to a heartbeat timeout, pass a heartbeat_error status code --- src/main/kotlin/org/phoenixframework/PhxSocket.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/org/phoenixframework/PhxSocket.kt b/src/main/kotlin/org/phoenixframework/PhxSocket.kt index c8f1125..119f81b 100644 --- a/src/main/kotlin/org/phoenixframework/PhxSocket.kt +++ b/src/main/kotlin/org/phoenixframework/PhxSocket.kt @@ -26,6 +26,9 @@ const val DEFAULT_HEARTBEAT: Long = 30000 /** The code used when the socket was closed without error */ const val WS_CLOSE_NORMAL = 1000 +/** The code used when the socket was closed after the heartbeat timer timed out */ +const val WS_CLOSE_HEARTBEAT_ERROR = 5000 + open class PhxSocket( url: String, params: Payload? = null, @@ -157,7 +160,7 @@ open class PhxSocket( /** * Disconnects the Socket */ - fun disconnect() { + fun disconnect(code: Int = WS_CLOSE_NORMAL) { connection?.close(WS_CLOSE_NORMAL, null) connection = null @@ -423,7 +426,7 @@ open class PhxSocket( pendingHeartbeatRef?.let { pendingHeartbeatRef = null logItems("Transport: Heartbeat timeout. Attempt to re-establish connection") - disconnect() + disconnect(WS_CLOSE_HEARTBEAT_ERROR) return@schedule }