How to fix error: WebSocket is already in CLOSING or CLOSED state


Some errors you can see:

Example 1:

Example 2:

Example 3:

How to fix this error?

Server: if you use nodejs

If you use socket.io, use version 2.0.3

npm i socket.io@2.0.3

If you use ws, use version from 0.4.25 to 0.4.32

npm i ws@0.4.32

You can try with other versions.

Update: 4/27/2022
Client: try to reconnect , example:

ar exampleSocket = new WebSocket("ws://5.111.72.222/test/ws")
 
exampleSocket.onclose = function() {
     //reconnect
	exampleSocket = new WebSocket("ws://5.111.72.222/test/ws")
}

Leave a Reply