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
相关问题
java如何获取屏幕宽。找不到api?
3885浏览 • 1回复 待解决
ohos.net.socket (Socket连接)链接不上
3591浏览 • 1回复 待解决
收不到
2981浏览 • 1回复 待解决
冷启动报错Error message
144浏览 • 1回复 待解决
native创建socket会失败
153浏览 • 1回复 待解决
openharmony 基于arkts的socket连接问题
2034浏览 • 1回复 待解决
app启动crash报错Error message:MainPage:
144浏览 • 1回复 待解决
Socket连接报错,错误码88
338浏览 • 1回复 待解决
Socket连接中支持发送哪些数据类型
326浏览 • 1回复 待解决
有没有基于socket封装成熟的网络库?
135浏览 • 1回复 待解决