WebSocket is already in CLOSING or CLOSED state Socket io

南笙酒味 提交于 2020-04-30 08:21:17

问题


Socket io returning error as: WebSocket is already in CLOSING or CLOSED state Socket io.

Node JS:

socket.to(socketId).emit('details', { userdetails: username });

React JS:

socket.on('details', data => {
    console.log(data.userdetails);
});

Can you please help me to resolve issue of WebSocket is in closing or closed state. Any kind of help is appreciable.


回答1:


1) Roll back to socket.io-client version 2.0.3. There is an issue in the latest 2.1.1 version.

npm install socket.io-client@2.0.3 --save

2) Also, make sure you cleanup after yourself in React.js and close the socket...

  componentDidMount() {
    this.socket = io('http://www.example.com');
  }

  componentWillUnmount() {
    this.socket.close();
  }



回答2:


There is a change for the default pingTimeout from 60000 (v2.0.4) to 5000 (v2.1.0+) which is not enough for some browsers like Chrome.

The solution to this issue on v2.1.0+ including the latest v2.2.0 is to override the default pingTimeout on your server.

const io = require('socket.io')(server, {
  pingTimeout: 60000,
});


来源:https://stackoverflow.com/questions/53230858/websocket-is-already-in-closing-or-closed-state-socket-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!