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) } }