HarmonyOS 鸿蒙Next中websoket 连接报错:client connection error HS: PROTOCOL malformed
HarmonyOS 鸿蒙Next中websoket 连接报错:client connection error HS: PROTOCOL malformed
let ws = webSocket.createWebSocket(); // 创建WebSocket实例
// 连接建立成功
ws.on('open', (err: BusinessError, value: Object) => {
if (!err) {
console.log("连接已建立");
// 可在此发送消息
// ws.send("Hello, server!", (err: BusinessError) => { /* 处理发送结果 */ });
}
});
// 接收服务器消息
ws.on('message', (err: BusinessError, value: string | ArrayBuffer) => {
if (value === 'bye') { // 收到特定消息时关闭连接
ws.close((err: BusinessError) => { /* 处理关闭结果 */ });
}
});
// 连接关闭
ws.on('close', (err: BusinessError, value: webSocket.CloseResult) => {
console.log(`关闭原因:${value.reason}`);
});
// 错误处理
ws.on('error', (err: BusinessError) => {
console.error("发生错误:" + JSON.stringify(err));
});
let url = "wss://192.168.0.71:4443/?roomId=chensong&peerId=xiqhlyrn"; // 替换为实际URL
let options: webSocket.WebSocketRequestOptions = {
header:{
"Sec-WebSocket-Protocol": "protoo"
},
// protocol:{
// "Sec-WebSocket-Protocol": "protoo"
// },
};
// WebSocketRequestOptions.protocol = {"Sec-WebSocket-Protocol","protoo"};
ws.connect(url,options, (err: BusinessError, value: boolean) => {
if (!err && value) {
console.log("连接请求创建成功");
}
});
12-16 11:06:57.622 15206-15899 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 105
12-16 11:06:57.623 15206-15899 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 19
12-16 11:06:57.623 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:169] websocket run service start
12-16 11:06:57.623 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 31
12-16 11:06:57.624 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 71
12-16 11:06:57.624 15206-15206 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:723] ConnectCallback connect success
12-16 11:06:57.624 15206-15206 A03D00/com.exa…tc_ohos/JSAPP com.examp…rtc_ohos I 连接请求创建成功
12-16 11:06:57.650 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 29
12-16 11:06:57.662 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 58
12-16 11:06:57.663 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 24
12-16 11:06:57.663 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:235] add header OK
12-16 11:06:57.698 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 44
12-16 11:06:57.698 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 1
12-16 11:06:57.698 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:332] Lws client connection error HS: PROTOCOL malformed
12-16 11:06:57.698 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:310] OnError 200
12-16 11:06:57.698 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:316] OnConnectError SetThreadStop
12-16 11:06:57.698 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 30
12-16 11:06:57.698 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos E [websocket_exec.cpp:179] lws_service stop
12-16 11:06:57.698 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:466] lws callback reason is 28
12-16 11:06:57.698 15206-15904 C015B0/com.exa…ohos/NETSTACK com.examp…rtc_ohos I [websocket_exec.cpp:183] websocket run service end
12-16 11:06:57.699 15206-15206 A03D00/com.exa…tc_ohos/JSAPP com.examp…rtc_ohos E 发生错误:{“code”:200,“data”:“101”}
作为客户端连接服务端,connect 成功后出现报错
Lws client connection error HS: PROTOCOL malformed
发生错误:{“code”:200,“data”:“101”}
后台日志显示:connection “close” event [conn:WSS:[10.20.0.102]:35552, code:1006, reason:“Connection dropped by remote peer.”] +12ms
怀疑是客户端的代码有问题
更多关于HarmonyOS 鸿蒙Next中websoket 连接报错:client connection error HS: PROTOCOL malformed的实战教程也可以访问 https://www.itying.com/category-93-b0.html
出现"PROTOCOL malformed"错误的核心原因在于WebSocket握手阶段的协议协商失败
错误现象:错误码101表示TLS握手失败
可能原因:自签名证书未通过验证或证书链不完整
// 尝试直接指定协议名
let options: webSocket.WebSocketRequestOptions = {
protocol: "protoo", // 直接指定协议名
header: { /* 其他自定义头 */ }
};
let options: webSocket.WebSocketRequestOptions = {
protocol: "protoo",
extraHeaders: "ignoreSSLVerification: true" // 开发环境可临时跳过证书验证
};
更多关于HarmonyOS 鸿蒙Next中websoket 连接报错:client connection error HS: PROTOCOL malformed的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
static struct WebSocket *client = nullptr;
client = OH_WebSocketClient_Constructor(onOpen, onMessage, onError, onClose);
if (client == nullptr) {
}
struct WebSocket_RequestOptions options;
options.headers = new WebSocket_Header();
options.headers->fieldName = "Sec-WebSocket-Protocol";
options.headers->fieldValue = "protoo";
options.headers->next = nullptr;
int connectRet = OH_WebSocketClient_AddHeader(client, *(options.headers));
connectRet = OH_WebSocketClient_Connect(client, "wss://192.168.0.71:4443/?roomId=xx", options);
你好,我用c++的代码来连接后台,也一样出现了报错 Lws client connection error HS: PROTOCOL malformed
发生错误:{“code”:200,“data”:“101”}
但是我再c++里面找不到类似的写法: let options: webSocket.WebSocketRequestOptions = { protocol: “protoo”, };
你好,可以试试在建立websocket连接时,增加参数 skipServerCertVerification,用于跳过服务器证书验证。
该API 20+可用。参考文档
| skipServerCertVerification | boolean | 否 | 是 | 是否跳过服务器证书验证。true表示跳过服务器证书验证,false表示不跳过服务器证书验证。默认为false。 |
ws.connect('wss:ipxxx', { skipServerCertVerification: true },
(err: BusinessError, value: boolean) => {
});
鸿蒙Next中WebSocket连接报错"PROTOCOL malformed"通常由协议格式错误导致。可能原因包括:握手请求头格式不符合RFC6455规范、Sec-WebSocket-Key等关键字段缺失或格式错误、服务器响应不符合WebSocket协议标准。需检查客户端握手请求的HTTP头格式、验证服务器响应头的完整性,确保遵循标准的WebSocket协议握手流程。
这个错误 HS: PROTOCOL malformed 通常表明WebSocket握手协议阶段出现了问题。根据你提供的代码和日志,核心问题在于 Sec-WebSocket-Protocol 子协议的设置方式不正确。
在HarmonyOS Next的WebSocket API中,Sec-WebSocket-Protocol 应该通过 protocol 字段设置,而不是 header 字段。你的代码注释中已经接近正确方案,但实现有误。
问题分析:
- 日志显示连接已成功建立(
connect success),但在握手阶段失败(lws callback reason is 44和1对应握手处理)。 - 错误码
101通常表示协议错误。 - 服务器日志
code:1006也表明连接因协议问题被对端关闭。
解决方案:
修改 WebSocketRequestOptions 的配置,将 Sec-WebSocket-Protocol 从 header 移到 protocol 字段:
let options: webSocket.WebSocketRequestOptions = {
protocol: ["protoo"] // 正确设置子协议
// 移除 header 中的 Sec-WebSocket-Protocol 设置
};
关键点说明:
protocol字段接受字符串数组,用于指定一个或多个子协议。header字段用于设置其他HTTP头,但Sec-WebSocket-Protocol是WebSocket握手专用的协议协商头,必须通过protocol字段设置。- 确保服务器端也支持并期望 “protoo” 这个子协议。
修改后,WebSocket库会在握手阶段正确协商子协议,避免协议格式错误。

