socket.TCPSocketConnection on('message')有概率无法响应

socket.TCPSocketConnection on('message')有概率无法响应

HarmonyOS
2024-05-10 18:10:39
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
youganlan
serverSocket.on(“connect”, (clientSocket: socket.TCPSocketConnection) => { 
  console.log(waitForRequest connect ${clientSocket.clientId}); // 1 
clientSocket.on(“message”, (data) => { // MessageCallBackBean 
  this.onMessage(clientSocket, data); // 2 
}); 
clientSocket.on(“error”, (err: Error) => { 
  console.log(waitForRequest-clientSocket-error,name=${err.name},msg=${err.message},stackTrace=${err.stack}); 
// closeSocket(clientSocket, false) 
}) 
clientSocket.on(“close”, () => { 
console.log(“waitForRequest-clientSocket-connection-close”); 
// closeSocket(clientSocket, true) 
}) 
console.log(waitForRequest connect2 ${clientSocket.clientId}); 
})

这个方法偶现无法正常响应message方法,也就是// 1处能正常打印日志,// 2处日志无法正常打印,导致无法请求数据无法传输;

2. httpRequest.request方法无法返回正常值,均收到timeout的exceptionawait httpRequest.requestInStream(url, option)

两个问题:

1. connection.destroy(); 不能写在finally里面,要写在then和catch里面

2. onMessage方法里面,data格式不正确,使用类似这种是可以的。 const data = “HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello, World!”;

修改之后运行成功的日志

02-01 14:13:53.244 22598-22985 C015b0/NetMgrSubsystem com.examp…lication I NETSTACK [socket_exec.cpp 1742] manager is ready with registering message event 
02-01 14:13:53.244 22598-22598 A03d00/JSAPP com.examp…lication I waitForRequest connect2 2 
02-01 14:13:53.244 22598-22985 C015b0/NetMgrSubsystem com.examp…lication I NETSTACK [socket_exec.cpp 1780] ClientRecv: fd is 70, buf is GET /ping HTTP/1.1 
02-01 14:13:53.246 22598-22598 A03d00/JSAPP com.examp…lication I waitForRequest-clientSocket-message,remoteInfo={“message”:{},“remoteInfo”:{“address”:“127.0.0.1”,“family”:“IPv4”,“port”:47086,“size”:121}} 
02-01 14:13:53.246 22598-22827 C015b0/NetMgrSubsystem com.examp…lication I NETSTACK [http_response.cpp 49] HEAD: HTTP/1.1 200 OK 
02-01 14:13:53.247 22598-22985 C015b0/NetMgrSubsystem com.examp…lication I NETSTACK [socket_exec.cpp 1780] ClientRecv: fd is 70, buf is , size is 0 bytes
分享
微博
QQ
微信
回复
2024-05-11 09:49:19
相关问题
Socket概率收不到message
156浏览 • 1回复 待解决
ohos.net.socket (Socket连接)链接不上
3593浏览 • 1回复 待解决
冷启动报错Error message
148浏览 • 1回复 待解决
native创建socket会失败
163浏览 • 1回复 待解决
app启动crash报错Error message:MainPage:
144浏览 • 1回复 待解决
TextInput的defaultFocus不响应
178浏览 • 1回复 待解决
应用中http请求响应2300023
565浏览 • 1回复 待解决
openharmony 基于arkts的socket连接问题
2037浏览 • 1回复 待解决
arkTS无法创建子窗口了解的吗?
933浏览 • 0回复 待解决
有没有基于socket封装成熟的网络库?
136浏览 • 1回复 待解决