Skip to content
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
5 changes: 4 additions & 1 deletion src/main/kotlin/org/phoenixframework/Socket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ const val WS_CLOSE_NORMAL = 1000
/** The socket was closed due to a SocketException. Likely the client lost connectivity */
const val WS_CLOSE_SOCKET_EXCEPTION = 4000

/** The socket was closed due to an SSLException. Likely the client lost connectivity */
const val WS_CLOSE_SSL_EXCEPTION = 4001

/** The socket was closed due to an EOFException. Likely the server abruptly closed */
const val WS_CLOSE_EOF_EXCEPTION = 4001
const val WS_CLOSE_EOF_EXCEPTION = 4002

/**
* Connects to a Phoenix Server
Expand Down
8 changes: 5 additions & 3 deletions src/main/kotlin/org/phoenixframework/Transport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import okhttp3.Response
import okhttp3.WebSocket
import okhttp3.WebSocketListener
import java.io.EOFException
import java.net.ConnectException
import java.net.SocketException
import java.net.URL
import javax.net.ssl.SSLException

/**
* Interface that defines different types of Transport layers. A default {@link WebSocketTransport}
Expand Down Expand Up @@ -137,9 +137,11 @@ class WebSocketTransport(

// Check if the socket was closed for some recoverable reason
if (t is SocketException) {
this.onClosed(webSocket, WS_CLOSE_SOCKET_EXCEPTION, "Socket Exception")
this.onClosed(webSocket, WS_CLOSE_SOCKET_EXCEPTION, "SocketException")
} else if (t is SSLException) {
this.onClosed(webSocket, WS_CLOSE_SSL_EXCEPTION, "SSLException")
} else if (t is EOFException) {
this.onClosed(webSocket, WS_CLOSE_EOF_EXCEPTION, "EOF Exception")
this.onClosed(webSocket, WS_CLOSE_EOF_EXCEPTION, "EOFException")
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/test/kotlin/org/phoenixframework/WebSocketTransportTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.mockito.MockitoAnnotations
import java.io.EOFException
import java.net.SocketException
import java.net.URL
import javax.net.ssl.SSLException

class WebSocketTransportTest {

Expand Down Expand Up @@ -96,6 +97,19 @@ class WebSocketTransportTest {
verify(mockOnClose).invoke(4000)
}

@Test
fun `onFailure also triggers onClose for SSLException`() {
val mockOnError = mock<(Throwable, Response?) -> Unit>()
val mockOnClose = mock<(Int) -> Unit>()
transport.onClose = mockOnClose
transport.onError = mockOnError

val throwable = SSLException("t")
transport.onFailure(mockWebSocket, throwable, mockResponse)
verify(mockOnError).invoke(throwable, mockResponse)
verify(mockOnClose).invoke(4001)
}

@Test
fun `onFailure also triggers onClose for EOFException`() {
val mockOnError = mock<(Throwable, Response?) -> Unit>()
Expand All @@ -106,7 +120,7 @@ class WebSocketTransportTest {
val throwable = EOFException()
transport.onFailure(mockWebSocket, throwable, mockResponse)
verify(mockOnError).invoke(throwable, mockResponse)
verify(mockOnClose).invoke(4001)
verify(mockOnClose).invoke(4002)
}

@Test
Expand Down