为什么react Js使用socket.io从服务器接收多个答案?
当将事件发布到WebSocket服务器时,服务器一次接收事件并发送响应,但是客户端多次接收事件: 结果预期 收到的结果 客户端:
client.emit("test", {
room: props.room,
});
client.on("testResponse", () => {
console.log("test response app");
});
服务器端:
socket.on("test", (data: { room: string }) => {
console.log("test");
io.in(data.room).emit("testResponse", {});
});
When emit a event to the websocket server, the server receive the event once and send a response, but the client receive the event multiple time:
result expected
result received
client side:
client.emit("test", {
room: props.room,
});
client.on("testResponse", () => {
console.log("test response app");
});
server side :
socket.on("test", (data: { room: string }) => {
console.log("test");
io.in(data.room).emit("testResponse", {});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:
在客户端,我必须将socket.on放在组件的使用效率中,然后在使用效果的返回中添加socket.off。
UPDATE:
on the client side I got to put the socket.on('response,()=>{...}) in the UseEffect of the component and add a socket.off in the return of the useEffect