Skip to content

Commit

Permalink
Merge pull request #2 from divyanshupundir/message-bytes
Browse files Browse the repository at this point in the history
feat: Add binary message support
  • Loading branch information
divyanshupundir authored Aug 9, 2023
2 parents 8c132ec + 07ab762 commit 98a9369
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ 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) {
onEvent.onEvent(WebSocket.Event.Open)
}

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) {
Expand Down
23 changes: 21 additions & 2 deletions websockt/src/main/kotlin/com/divpundir/websockt/WebSocket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 21 additions & 2 deletions websockt/src/main/kotlin/com/divpundir/websockt/WebSocketClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}
}
}
)
Expand Down

0 comments on commit 98a9369

Please sign in to comment.