diff --git a/build.gradle.kts b/build.gradle.kts index 0c420f8..c7f68bd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -47,7 +47,7 @@ allprojects { licenses { license { name.set("The Apache Software License, Version 2.0") - url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") + url.set("https://www.apache.org/licenses/LICENSE-2.0.txt") distribution.set("repo") } } diff --git a/buildSrc/src/main/kotlin/Configuration.kt b/buildSrc/src/main/kotlin/Configuration.kt index 5db6390..1431365 100644 --- a/buildSrc/src/main/kotlin/Configuration.kt +++ b/buildSrc/src/main/kotlin/Configuration.kt @@ -2,6 +2,6 @@ import org.gradle.api.JavaVersion object Config { const val group = "com.divpundir.websockt" - const val version = "0.2.0" + const val version = "0.3.0" val javaVersion = JavaVersion.VERSION_1_8 } diff --git a/engine-okhttp/src/main/kotlin/com/divpundir/websockt/engine/okhttp/OkHttpWebSocketListener.kt b/engine-okhttp/src/main/kotlin/com/divpundir/websockt/engine/okhttp/OkHttpWebSocketListener.kt index 444353e..29086bf 100644 --- a/engine-okhttp/src/main/kotlin/com/divpundir/websockt/engine/okhttp/OkHttpWebSocketListener.kt +++ b/engine-okhttp/src/main/kotlin/com/divpundir/websockt/engine/okhttp/OkHttpWebSocketListener.kt @@ -2,12 +2,13 @@ package com.divpundir.websockt.engine.okhttp import com.divpundir.websockt.WebSocket import okhttp3.Response +import okio.ByteString import okhttp3.WebSocket as DelegateSocket import okhttp3.WebSocketListener as DelegateListener public class OkHttpWebSocketListener( private val onFailure: WebSocket.FailureListener, - private val onEvent: WebSocket.Event.Listener + private val onEvent: WebSocket.Event.Listener, ) : DelegateListener() { override fun onOpen(webSocket: DelegateSocket, response: Response) { @@ -15,7 +16,11 @@ public class OkHttpWebSocketListener( } override fun onMessage(webSocket: DelegateSocket, text: String) { - onEvent.onEvent(WebSocket.Event.Message(text)) + onEvent.onEvent(WebSocket.Event.Message.Text(text)) + } + + override fun onMessage(webSocket: DelegateSocket, bytes: ByteString) { + onEvent.onEvent(WebSocket.Event.Message.Bytes(bytes.toByteArray())) } override fun onClosing(webSocket: DelegateSocket, code: Int, reason: String) { diff --git a/websockt/src/main/kotlin/com/divpundir/websockt/WebSocket.kt b/websockt/src/main/kotlin/com/divpundir/websockt/WebSocket.kt index 86e1687..0ec13e8 100644 --- a/websockt/src/main/kotlin/com/divpundir/websockt/WebSocket.kt +++ b/websockt/src/main/kotlin/com/divpundir/websockt/WebSocket.kt @@ -10,12 +10,31 @@ public interface WebSocket { public object Open : Event - public data class Message(val payload: String) : Event - public data class Closing(val code: Int, val reason: String?) : Event public data class Close(val code: Int, val reason: String?) : Event + public sealed interface Message : Event { + + public data class Text(val payload: String) : Message + + public data class Bytes(val payload: ByteArray) : Message { + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as Bytes + + return payload.contentEquals(other.payload) + } + + override fun hashCode(): Int { + return payload.contentHashCode() + } + } + } + public fun interface Listener { public fun onEvent(event: Event) diff --git a/websockt/src/main/kotlin/com/divpundir/websockt/WebSocketClient.kt b/websockt/src/main/kotlin/com/divpundir/websockt/WebSocketClient.kt index b6130f4..5d81d47 100644 --- a/websockt/src/main/kotlin/com/divpundir/websockt/WebSocketClient.kt +++ b/websockt/src/main/kotlin/com/divpundir/websockt/WebSocketClient.kt @@ -12,12 +12,31 @@ public interface WebSocketClient { public object Open : Event - public data class Message(val payload: String) : Event - public data class Closing(val code: Int, val reason: String?) : Event public data class Close(val code: Int, val reason: String?) : Event + public sealed interface Message : Event { + + public data class Text(val payload: String) : Message + + public data class Bytes(val payload: ByteArray) : Message { + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as Bytes + + return payload.contentEquals(other.payload) + } + + override fun hashCode(): Int { + return payload.contentHashCode() + } + } + } + public fun interface Listener { public fun onEvent(event: Event) diff --git a/websockt/src/main/kotlin/com/divpundir/websockt/WebSocketClientImpl.kt b/websockt/src/main/kotlin/com/divpundir/websockt/WebSocketClientImpl.kt index 69afc41..20c945c 100644 --- a/websockt/src/main/kotlin/com/divpundir/websockt/WebSocketClientImpl.kt +++ b/websockt/src/main/kotlin/com/divpundir/websockt/WebSocketClientImpl.kt @@ -3,7 +3,7 @@ package com.divpundir.websockt internal class WebSocketClientImpl( private val factory: WebSocketFactory, private val onFailure: WebSocketClient.FailureListener, - private val onEvent: WebSocketClient.Event.Listener + private val onEvent: WebSocketClient.Event.Listener, ) : WebSocketClient { @Volatile @@ -23,9 +23,14 @@ internal class WebSocketClientImpl( onEvent = { when (it) { is WebSocket.Event.Open -> onEvent.onEvent(WebSocketClient.Event.Open) - is WebSocket.Event.Message -> onEvent.onEvent(WebSocketClient.Event.Message(it.payload)) is WebSocket.Event.Closing -> onEvent.onEvent(WebSocketClient.Event.Closing(it.code, it.reason)) is WebSocket.Event.Close -> onEvent.onEvent(WebSocketClient.Event.Close(it.code, it.reason)) + is WebSocket.Event.Message -> { + when (it) { + is WebSocket.Event.Message.Text -> onEvent.onEvent(WebSocketClient.Event.Message.Text(it.payload)) + is WebSocket.Event.Message.Bytes -> onEvent.onEvent(WebSocketClient.Event.Message.Bytes(it.payload)) + } + } } } )