问题
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