Square's meticulous HTTP client for Java and Kotlin
—
WebSocket protocol support for real-time bidirectional communication.
interface WebSocket {
fun request(): Request
fun queueSize(): Long
fun send(text: String): Boolean
fun send(bytes: ByteString): Boolean
fun close(code: Int, reason: String?): Boolean
fun cancel()
}abstract class WebSocketListener {
open fun onOpen(webSocket: WebSocket, response: Response) {}
open fun onMessage(webSocket: WebSocket, text: String) {}
open fun onMessage(webSocket: WebSocket, bytes: ByteString) {}
open fun onClosing(webSocket: WebSocket, code: Int, reason: String) {}
open fun onClosed(webSocket: WebSocket, code: Int, reason: String) {}
open fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {}
}val listener = object : WebSocketListener() {
override fun onMessage(webSocket: WebSocket, text: String) {
println("Received: $text")
}
}
val request = Request.Builder().url("wss://echo.websocket.org").build()
val webSocket = client.newWebSocket(request, listener)
webSocket.send("Hello WebSocket!")Install with Tessl CLI
npx tessl i tessl/maven-com-squareup-okhttp3--okhttp