Socket有高概率收不到message

根据文档https://www.seaxiang.com/blog/2d691b22a889468680b455c7dd5a8cdb,有比较高的概率收不到client.on(“message”, (value: SocketInfo) 中的回调

具体代码如下:

let tcpServer = socket.constructTCPSocketServerInstance(); 
 
tcpServer.on(“connect”, (client: socket.TCPSocketConnection) => { 
// Subscribe to events of the TCPSocketConnection object. 
client.on(“close”, () => { 
console.log(“on close success”); 
}); 
client.on(“message”, (value: SocketInfo) => { 
// 此处高概率收不到message 
let buffer = value.message; 
let dataView = new DataView(buffer); 
let str = “”; 
for (let i = 0; i < dataView.byteLength; ++i) { 
str += String.fromCharCode(dataView.getUint8(i)); 
} 
console.log(“received message–:” + str); 
});
HarmonyOS
2024-05-09 23:32:59
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
e_leaner

定义一个数组,在有客户端连接上来的时候,将客户端的socket加入数组中,避免被回收释放,导致无法接收数据。代码如下:

// 定义存放客户端连接的数组 
let tcpConnectArray: socket.TCPSocketConnection[] = []; 
 
tcpServer.on(“connect”, (client: socket.TCPSocketConnection) => { 
// 保存客户端的socket 
tcpConnectArray.push(client); 
// Subscribe to events of the TCPSocketConnection object. 
client.on(“close”, () => { 
console.log(“on close success”); 
}); 
client.on(“message”, (value: SocketInfo) => { 
// 此处高概率收不到message 
let buffer = value.message; 
let dataView = new DataView(buffer); 
let str = " "; 
for (let i = 0; i < dataView.byteLength; ++i) { 
str += String.fromCharCode(dataView.getUint8(i)); 
} 
console.log("received message–:" + str); 
});
分享
微博
QQ
微信
回复
2024-05-10 11:24:04
相关问题
HarmonyOS Push Kit收不到推送消息了
412浏览 • 1回复 待解决
java如何获取屏幕宽。找不到api?
5430浏览 • 1回复 待解决
ohos.net.socket (Socket连接)链接不上
5423浏览 • 1回复 待解决
概率性报错5400104、5400103怎么处理
580浏览 • 1回复 待解决
收不到
4618浏览 • 1回复 待解决
native创建socket会失败
1702浏览 • 1回复 待解决
冷启动报错Error message
1981浏览 • 1回复 待解决
socket连接NetAddress获取问题
320浏览 • 1回复 待解决
Socket接口库可以绑定域名吗
447浏览 • 1回复 待解决
HarmonyOS 经典蓝牙的socket通信问题
261浏览 • 1回复 待解决
在native层创建socket异常
313浏览 • 1回复 待解决