HarmonyOS鸿蒙Next中@ohos/socketio_2.x、@ohos/socketio这两个库连接不上,一直reconnecting
HarmonyOS鸿蒙Next中@ohos/socketio_2.x、@ohos/socketio这两个库连接不上,一直reconnecting
两个都跑不通,用html、安卓连我的服务端都是能连上的,gitee仓库已经关闭了,提不了问题,这里翻遍了也没找到啥原因
https://gitee.com/openharmony-tpc/openharmony_tpc_samples
// @State serverUrl: string = 'http://wbb-local.xxx.xxx:8080'
@State serverUrl: string = 'https://wbb-local.xxx.xxx:9443'
client: client_socket | null = null
ws: webSocket.WebSocket = webSocket.createWebSocket();
aboutToAppear(): void {
this.client = new client_socket(this.serverUrl)
this.client.connect(this.serverUrl)
this.client.set_open_listener(this.on_open.bind(this))
this.client.set_fail_listener(this.on_fail.bind(this));
this.client.set_reconnecting_listener(this.on_reconnecting.bind(this));
this.client.set_close_listener(this.on_close.bind(this));
this.client.set_socket_open_listener(this.on_socket_open.bind(this));
this.client.set_socket_close_listener(this.on_socket_close.bind(this));
}
看日志就是一直在连接、断开连接

更多关于HarmonyOS鸿蒙Next中@ohos/socketio_2.x、@ohos/socketio这两个库连接不上,一直reconnecting的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更多关于HarmonyOS鸿蒙Next中@ohos/socketio_2.x、@ohos/socketio这两个库连接不上,一直reconnecting的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,@ohos/socketio_2.x
和@ohos/socketio
库连接不上并一直处于reconnecting
状态,可能是由于网络配置、服务器端问题或库版本不兼容导致的。检查网络连接是否正常,确保服务器端配置正确,并确认使用的库版本与鸿蒙Next系统兼容。如果问题依旧,建议查看日志以获取更多错误信息。
从你的描述和代码来看,在HarmonyOS Next中使用Socket.IO库连接服务端时出现持续重连的问题。以下是可能的原因和解决方案:
- 证书问题:
- 你使用的是HTTPS(9443端口),但HarmonyOS可能需要额外配置信任证书
- 尝试在开发阶段先用HTTP(8080端口)测试
- 协议版本不匹配:
- 确保客户端和服务端使用的Socket.IO协议版本完全一致
- 你尝试了2.x和4.x两个版本,建议统一使用4.x版本
- 网络权限配置:
- 检查config.json中是否已添加网络权限:
"reqPermissions": [
{
"name": "ohos.permission.INTERNET"
}
]
- 连接参数问题:
- 尝试添加明确的连接参数:
this.client = new client_socket(this.serverUrl, {
transports: ['websocket'],
reconnectionAttempts: 5,
timeout: 20000
});
- 调试建议:
- 在on_fail回调中打印详细错误信息
- 使用抓包工具(Wireshark等)检查实际网络请求
- 检查服务端日志看是否收到连接请求
如果问题仍然存在,建议提供完整的错误日志和on_fail回调中的错误信息,以便进一步分析具体原因。