Receiving Error: xhr poll error socket.io client React

Receiving Error: xhr poll error socket.io client React

我在尝试连接到我的 websocket 网关时在 connect_error 事件中收到 Error: xhr poll error。 我正在使用 "socket.io-client": "^4.2.0".

import { io } from "socket.io-client";

const ENDPOINT = "http://localhost:3001";

const socket = io(ENDPOINT);

socket.on("connect_error", (e: any) => {
   console.log(e);
});

您可以尝试将客户端连接设置为仅使用 websocket 传输。默认情况下,它使用 weboscket 和轮询。

{
   transports: ['websocket']
}

因此您的代码将变为:

const socket = io(ENDPOINT, {
   transports: ['websocket']
});