QRSender/app/src/main/java/com/zervonetwork/qrsender/WSClient.kt
2023-09-24 19:10:53 +02:00

46 lines
1.3 KiB
Kotlin

import android.util.Log
import okhttp3.*
import java.util.concurrent.TimeUnit
class WSClient(private val serverUrl: String, private val port: Int) {
private var webSocket: WebSocket? = null
init {
val client = OkHttpClient.Builder()
.readTimeout(0, TimeUnit.MILLISECONDS)
.build()
val url = "ws://$serverUrl:$port"
val request = Request.Builder()
.url(url)
.build()
webSocket = client.newWebSocket(request, object : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: Response) {
Log.d("WSClient", "WebSocket connection opened")
}
override fun onMessage(webSocket: WebSocket, text: String) {
Log.d("WSClient", "Received message: $text")
}
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
Log.d("WSClient", "WebSocket connection closed")
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
Log.e("WSClient", "WebSocket connection failure", t)
}
})
}
fun send(message: String) {
webSocket?.send(message)
}
fun close() {
webSocket?.close(1000, null)
}
}