В моем build.gradle :
"com.squareup.okhttp3:okhttp:3.13.1",
'com.squareup.okhttp3:logging-interceptor:3.13.1',
Проект Kotlin open connect to websocket:
const val WEB_SOCKET_URL = "wss://api.lbkex.com/ws/V2/";
fun main(args: Array<String>) {
val оkHttpClient = OkHttpClient()
val wsListener = object : WebSocketListener() {
override fun onOpen(ws: WebSocket, response: Response) {
super.onOpen(ws, response)
val requestJson = """{"action": "subscribe", "subscribe": "tick", "pair": "vtho_eth"}"""
ws.send(requestJson)
}
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
super.onClosing(webSocket, code, reason)
logger.info("ws Closing, reason = " + reason)
}
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
super.onClosed(webSocket, code, reason)
logger.info("ws Closed")
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
super.onFailure(webSocket, t, response)
logger.error("ws Failure ", t)
}
override fun onMessage(webSocket: WebSocket, text: String) {
super.onMessage(webSocket, text)
logger.info("ws Text message: $text")
}
override fun onMessage(webSocket: WebSocket, bytes: ByteString) {
super.onMessage(webSocket, bytes)
}
}
logger.info("Started")
val wsRequest = Request.Builder()
.url(WEB_SOCKET_URL)
.build()
val ws = оkHttpClient.newWebSocket(wsRequest, wsListener)
}
В результате я успешно открываю webscocket до wss://api.lbkex.com/ws/V2/
.
В качестве результата метод onMessage
вызывается много раз. Ницца.
Но через 15 минут вызовите метод onFailure
с ошибкой:
13:40:16.687 [OkHttp https://api.lbkex.com/...] ERROR MAIN_LOGGER - ws Failure
java.io.EOFException: null
at okio.RealBufferedSource.require(RealBufferedSource.java:61)
at okio.RealBufferedSource.readByte(RealBufferedSource.java:74)
at okhttp3.internal.ws.WebSocketReader.readHeader(WebSocketReader.java:117)
at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:101)
at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.java:274)
at okhttp3.internal.ws.RealWebSocket$2.onResponse(RealWebSocket.java:214)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)