diff --git a/src/main/kotlin/org/phoenixframework/PhxSocket.kt b/src/main/kotlin/org/phoenixframework/PhxSocket.kt index 119f81b..437ea67 100644 --- a/src/main/kotlin/org/phoenixframework/PhxSocket.kt +++ b/src/main/kotlin/org/phoenixframework/PhxSocket.kt @@ -99,7 +99,6 @@ open class PhxSocket( /// Timer to use when attempting to reconnect private var reconnectTimer: PhxTimer? = null - private val gson: Gson = GsonBuilder() .setLenient() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) @@ -154,8 +153,8 @@ open class PhxSocket( // Public //------------------------------------------------------------------------------ /** True if the Socket is currently connected */ - val isConnected: Boolean - get() = connection != null + var isConnected: Boolean = false + private set /** * Disconnects the Socket @@ -173,7 +172,7 @@ open class PhxSocket( */ fun connect() { // Do not attempt to reconnect if already connected - if (isConnected) return + if (connection != null) return connection = client.newWebSocket(request, this) } @@ -440,6 +439,7 @@ open class PhxSocket( // WebSocketListener //------------------------------------------------------------------------------ override fun onOpen(webSocket: WebSocket?, response: Response?) { + isConnected = true this.onConnectionOpened() } @@ -451,10 +451,12 @@ open class PhxSocket( } override fun onClosed(webSocket: WebSocket?, code: Int, reason: String?) { + isConnected = false this.onConnectionClosed(code) } override fun onFailure(webSocket: WebSocket?, t: Throwable, response: Response?) { + isConnected = false this.onConnectionError(t, response) } } diff --git a/src/test/kotlin/org/phoenixframework/PhxSocketTest.kt b/src/test/kotlin/org/phoenixframework/PhxSocketTest.kt index 317c8ef..851f3b7 100644 --- a/src/test/kotlin/org/phoenixframework/PhxSocketTest.kt +++ b/src/test/kotlin/org/phoenixframework/PhxSocketTest.kt @@ -1,7 +1,9 @@ package org.phoenixframework import com.google.common.truth.Truth.assertThat +import okhttp3.WebSocket import org.junit.Test +import org.mockito.Mockito class PhxSocketTest { @@ -36,4 +38,44 @@ class PhxSocketTest { assertThat(PhxSocket("wss://localhost:4000/socket/websocket", spacesParams).endpoint.toString()) .isEqualTo("https://localhost:4000/socket/websocket?user_id=1&token=abc%20123") } + + @Test + fun isConnected_isTrue_WhenSocketConnected() { + val mockSocket = Mockito.mock(WebSocket::class.java) + val socket = PhxSocket("http://localhost:4000/socket/websocket") + + socket.onOpen(mockSocket, null) + + assertThat(socket.isConnected).isTrue() + } + + @Test + fun isConnected_isFalse_WhenSocketNotYetConnected() { + val socket = PhxSocket("http://localhost:4000/socket/websocket") + + + assertThat(socket.isConnected).isFalse() + } + + @Test + fun isConnected_isFalse_WhenSocketFailed() { + val mockSocket = Mockito.mock(WebSocket::class.java) + val socket = PhxSocket("http://localhost:4000/socket/websocket") + + socket.onFailure(mockSocket, RuntimeException(), null) + + + assertThat(socket.isConnected).isFalse() + } + + @Test + fun isConnected_isFalse_WhenSocketClosed() { + val mockSocket = Mockito.mock(WebSocket::class.java) + val socket = PhxSocket("http://localhost:4000/socket/websocket") + + socket.onClosed(mockSocket, 0, "closed") + + + assertThat(socket.isConnected).isFalse() + } }