HarmonyOS 鸿蒙Next WebSocket添加header信息后连接失败问题

发布于 1周前 作者 htzhanglong 来自 鸿蒙OS

HarmonyOS 鸿蒙Next WebSocket添加header信息后连接失败问题

WebSocket  connect的时候不添加Header信息, 可以connect并open , 

12-17 14:42:51.411  3491  3491 I A00000/SocketManager: connectSocket, url = ws://219.147.31.58:9321/hl?user_id=/9999666622/0

12-17 14:42:51.412  3491  3491 I C015b0/NETSTACK: [module_template.h:97] js invoke WebSocketConnect

12-17 14:42:51.419  3491  5695 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 21

12-17 14:42:51.420  3491  5695 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 27

12-17 14:42:51.425  3491  5695 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 105

12-17 14:42:51.426  3491  5695 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 19

12-17 14:42:51.427  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:287] websocket run service start

12-17 14:42:51.427  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 31

12-17 14:42:51.427  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 71

12-17 14:42:51.427  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 71

12-17 14:42:51.427  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 71

12-17 14:42:51.446  3491  3491 I C015b0/NETSTACK: [websocket_exec.cpp:788] ConnectCallback connect success

12-17 14:42:51.446  3491  3491 I A00000/WebSocketDemo: connect sucess, isConnect = true

12-17 14:42:51.446  3491  3491 I A00000/SocketManager: onSocketConnect success

12-17 14:42:51.590  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 29

12-17 14:42:51.591  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 24

12-17 14:42:51.591  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:352] add header OK

12-17 14:42:51.733  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 44

12-17 14:42:51.733  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 2

12-17 14:42:51.733  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:1072] no event listener: headerReceive

12-17 14:42:51.733  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 3

12-17 14:42:51.733  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:986] OnOpen 101 Switching Protocols

12-17 14:42:51.733  3491  5698 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 10

12-17 14:42:51.734  3491  3491 I A00000/SocketManager: onSocketOpen{“status”:101,“message”:“Switching Protocols”}

当添加上Header信息

header: {
“Sec-WebSocket-Protocol”: “xxxxxxxx-signalling”
}再去connect时, 可以connect,但是无法open, 有如下报错, 不知道这个{“code”:200,“data”:“101”}异常是意思以及什么原因导致的.

12-17 17:14:23.062  4021  4021 I A00000/SocketManager: setSocketCb

12-17 17:14:23.062  4021  4021 I A00000/SocketManager: connectSocket, url = ws://219.147.31.58:9321/hl?user_id=/9999666621/0

12-17 17:14:23.062  4021  4021 I C015b0/NETSTACK: [module_template.h:97] js invoke WebSocketConnect

12-17 17:14:23.062  4021  4021 I C015b0/NETSTACK: [connect_context.cpp:162] ConnectContext CAPATH not found

12-17 17:14:23.062  4021  4021 I C015b0/NETSTACK: [connect_context.cpp:189] ConnectContext CLIENT_CERT not found

12-17 17:14:23.062  4021  4021 D C015b0/NETSTACK: [connect_context.cpp:209] websocket connect proxy not found, use system proxy

12-17 17:14:23.062  4021  4021 D C015b0/NETSTACK: [connect_context.cpp:257] websocket connect protocol not found

12-17 17:14:23.063  4021  4021 D C015b0/NETSTACK: [module_template.h:136] WebSocketConnect create promise

12-17 17:14:23.066  4021 24247 D C015b0/NETSTACK: [websocket_exec.cpp:728] websocket Connect exec

12-17 17:14:23.068  4021 24247 D C015b0/NETSTACK: [websocket_exec.cpp:709] load system CA

12-17 17:14:23.100  4021 24247 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 21

12-17 17:14:23.100  4021 24247 D C015b0/NETSTACK: [websocket_exec.cpp:655] tempOrigin = ws://219.147.31.58:9321

12-17 17:14:23.100  4021 24247 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 27

12-17 17:14:23.101  4021 24247 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 105

12-17 17:14:23.101  4021 24247 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 19

12-17 17:14:23.101  4021  4021 I C015b0/NETSTACK: [websocket_exec.cpp:788] ConnectCallback connect success

12-17 17:14:23.101  4021  4021 I A00000/WebSocketDemo: connect success, isConnect = true

12-17 17:14:23.101  4021  4021 I A00000/SocketManager: onSocketConnect success

12-17 17:14:23.103  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:287] websocket run service start

12-17 17:14:23.104  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 31

12-17 17:14:23.104  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 71

12-17 17:14:23.106  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 71

12-17 17:14:23.106  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 71

12-17 17:14:23.227  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 29

12-17 17:14:23.227  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 24

12-17 17:14:23.227  4021 24252 D C015b0/NETSTACK: [websocket_exec.cpp:329] lws callback client append handshake header

12-17 17:14:23.227  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:352] add header OK

12-17 17:14:23.345  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 44

12-17 17:14:23.345  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 1

12-17 17:14:23.345  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:449] Lws client connection error HS: PROTOCOL malformed

12-17 17:14:23.345  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:427] OnError 200

12-17 17:14:23.345  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:433] OnConnectError SetThreadStop

12-17 17:14:23.345  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 30

12-17 17:14:23.345  4021 24252 D C015b0/NETSTACK: [websocket_exec.cpp:553] lws callback wsi destroy

12-17 17:14:23.345  4021 24252 D C015b0/NETSTACK: [websocket_exec.cpp:190] set wsi nullptr

12-17 17:14:23.346  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:576] lws callback reason is 28

12-17 17:14:23.346  4021 24252 D C015b0/NETSTACK: [websocket_exec.cpp:570] lws callback protocol destroy

12-17 17:14:23.346  4021  4021 I A00000/WebSocketDemo: on err :err is{“code”:200,“data”:“101”}

12-17 17:14:23.346  4021  4021 I A00000/SocketManager: onSocketError"{“code”:200,“data”:“101”}"

12-17 17:14:23.346  4021  4021 I A00000/SocketManager: SocketManager onLocalConnect  code:200

12-17 17:14:23.346  4021 24252 I C015b0/NETSTACK: [websocket_exec.cpp:300] websocket run service end

3 回复

1.确认客户端和服务器端的WebSocket版本是否兼容

2.服务器是否支持协议"xxxxxxxx-signalling",以及请求头的格式是否正确(websocket的header应该是object,你用的json可能有问题)

3.查看服务器端日志,了解为何连接失败,并根据具体错误信息进行调试

下面的header信息是API 参考示例代码里的, 我的格式没有问题. { header:{ name1: ‘value1’, name2: ‘value2’, name3: ‘value3’ }, proxy: { host: ‘192.168.0.150’, port: 8888, exclusionList: [] }, protocol: ‘my-protocol’, }

我调整了一下Sec-WebSocket-Protocol的位置,这个信息不应该放到header里, 应该直接在protocol: 中添加, 另外我的header中又额外添加了一些 Java架构下的 信息, 现在不报错了

针对HarmonyOS 鸿蒙Next WebSocket添加header信息后连接失败的问题,这通常是由于WebSocket配置或权限设置不当导致的。以下是一些可能的解决步骤:

  1. 检查WebSocket配置

    • 确保WebSocket服务器支持添加header信息的请求。
    • 检查WebSocket客户端的配置,确保header信息被正确添加且格式无误。
  2. 检查网络权限

    • 确保应用已申请并获得了必要的网络权限,如INTERNET和GET_NETWORK_INFO。
    • 在鸿蒙系统中,通过配置文件或代码方式确保网络权限被正确设置。
  3. 检查服务器响应

    • 分析服务器对WebSocket连接请求的响应,查看是否有错误代码或错误信息。
    • 根据服务器响应调整WebSocket客户端的配置或代码。
  4. 使用原生WebSocket API

    • 如果使用的是鸿蒙系统提供的WebSocket API,尝试使用原生JavaScript的WebSocket API看是否能成功连接。
  5. 联系开发者支持

    • 如果以上步骤都无法解决问题,建议联系鸿蒙系统的开发者支持团队获取更专业的帮助。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部