gpt4 book ai didi

android - 通过 okHttp 套接字发送数据

转载 作者:行者123 更新时间:2023-12-03 23:04:07 26 4
gpt4 key购买 nike

我正在尝试连接 websocket并且测试套接字正在工作wss://echo.websocket.org使用我的代码,但我的 WebSocket 无法正常工作并且失败,但每当我尝试使用浏览器时,它都可以工作。
我的安卓代码

 // val request: Request = Request.Builder().url("wss://echo.websocket.org").build()
val request: Request = Request.Builder().url("wss://dev.xyz.co.uk/xyz").build()
val listener: EchoWebSocketListener = EchoWebSocketListener()
val ws: WebSocket = client.newWebSocket(request, listener)
client.dispatcher().executorService()

private inner class EchoWebSocketListener : WebSocketListener() {
private var NORMAL_CLOSURE_STATUS: Int = 1000

override fun onOpen(webSocket: WebSocket, response: Response) {
Log.d("onOpen-----", "Socket connected!!!!!")
webSocket.send("[5, \"driver106apikey\"]");
webSocket.close(NORMAL_CLOSURE_STATUS,"GoodBye!!")
}
override fun onMessage(webSocket: WebSocket, text: String) {
Log.d("onMessage-----", "Socket connected!!!!!")
Log.d("onMessage String-----", text)
output("Receiving------ "+ text)
}
override fun onMessage(webSocket: WebSocket, bytes: ByteString) {
Log.d("onMessage-----", "Socket onmessage!!!!!")
output("Receiving------ "+ bytes.toString())
}

override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
Log.d("onClosing-----", "Socket closing!!!!!")
webSocket.close(NORMAL_CLOSURE_STATUS, null)
output("Closing : "+ code + "/" + reason)
}

override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
Log.d("onFailure-----", "FAILURE")
Log.d("onFailure-----", t.message)

}
}
这就是数据在网络中的发送方式
1. When you send data you send in this format : 
function clickBtn() {
var data = {
event: 0,
data: {
lat: 123.2123,
lng: 22.5543,
status: 1
}
}

conn.publish('driver106apikey', data);
};
但是尝试在android中使用键值发送失败了。
 val formBody: RequestBody = FormBody.Builder()
.add("driver106apikey", "{
"event": 0,
"data": {
"lat": 123.2123,
"lng": 22.5543,
"status": 1
}
}")
.build()
enter image description here
在android中显示错误

FAILURE Expected HTTP 101 response but was '426 NoSec-WebSocket-Protocols requested supported'


这就是通过浏览器发送数据的方式。
autobahn.js
socket.htlm

最佳答案

OkHttp 似乎不支持 WAMP 协议(protocol)。我建议切换到 Autobahn|Java或类似的库。

关于android - 通过 okHttp 套接字发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63777035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com