【发布时间】:2021-04-15 15:49:47
【问题描述】:
我尝试将 React 客户端连接到我的 Socket.IO 服务器。我注意到 Socket.IO 客户端每 +/- 5 秒重新连接一次。当我尝试使用 vanilla html/js 简单应用程序做同样的事情时,一切正常。
React 组件内部:
useEffect(() => {
const s = getChatClient();
}, []);
socket.io-client 模块内部:
var chatClient;
export function getChatClient(
token = "secret"
) {
if (!chatClient) {
chatClient = io("http://localhost:5000/chat", {
query: {
token,
},
});
chatClient
.on("connect", () => {
chatClient.emit("textMessage", "123cos");
})
.on("hello", (msg) => {
console.log("12");
});
}
return chatClient;
}
顺便说一句:我知道我可以导出 const 等(我已更改为这个版本,因为我认为它会有所帮助)。
我尝试了不同的方法来解决这个问题,但我陷入了困境。有什么想法吗?
当我打开 html/js 简单客户端时从服务器登录:
15:30:00 User Ilona connected to the socket.io server on /
当我退出时:
15:29:12 User Ilona disconnected
使用 React 应用程序:
15:30:00 User Ilona connected to the socket.io server on '/'
15:30:05 User Ilona disconnected
15:30:10 User Ilona connected to the socket.io server on '/'
15:30:15 User Ilona disconnected
15:30:20 User Ilona connected to the socket.io server on '/'
15:30:25 User Ilona disconnected
此问题与组件重新渲染或类似问题无关。
我正在开发 MacOS Big Sur。
【问题讨论】: