【问题标题】:Using Socket-Io with react使用带有反应的 Socket-Io
【发布时间】:2021-11-22 15:32:47
【问题描述】:

我正在尝试将命名空间与 socket-io 服务器和 react 客户端一起使用。

客户

    const [socket, setSocket] = useState(null);

    useEffect(() => {
        const testSocket = io(`http://localhost:3001/poker`);
        console.log(testSocket)
        setSocket(testSocket);
        return () => testSocket.close();
    }, [setSocket]);

服务器

    io.on("connection", (socket) => {
        console.log('connection');
    });

    const test = io.of("/test");

    test.on('connection', socket => {
        console.log("here2");
    });

当我这样做时,客户端上的 console.log() 会返回 Object { connected: false, disconnected: true, receiveBuffer: [], sendBuffer: [], ids: 0, acks: {}, flags: {}, io: {…}, nsp: "/test", subs: (4) […] } 在服务器端它每隔几秒钟就会说一次“连接”,但它从不触发命名空间。 浏览器每隔几秒发送的请求具有轮询传输而不是套接字,因此它可能不是连接,但我不知道为什么,有什么想法吗?

【问题讨论】:

    标签: reactjs websocket socket.io


    【解决方案1】:

    在客户端设置 {transports: ["websocket"]} 似乎已经成功了。

    【讨论】:

      猜你喜欢
      • 2017-03-07
      • 2017-07-01
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-09-24
      相关资源
      最近更新 更多