Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket is already in CLOSING or CLOSED state. ERROR #592

Closed
litosh-web opened this issue Jan 20, 2021 · 11 comments
Closed

WebSocket is already in CLOSING or CLOSED state. ERROR #592

litosh-web opened this issue Jan 20, 2021 · 11 comments

Comments

@litosh-web
Copy link

litosh-web commented Jan 20, 2021

I'm faced with new error a few hours later. If I open my website, then connection will open. But If I switch tabs in my browser after 5 minutes, I'll get this error WebSocket is already in CLOSING or CLOSED state in my webbrowser's console and lose connection
I'm surfed the Internet and found a solution, that I should set PingTimeOut option on a client side. But how can I do this...
Also I seen Timer in the Workerman, but I'm doubt about relating this to my issue, because no information.

Similar Issue socketio/socket.io#3259

@walkor
Copy link
Owner

walkor commented Jan 21, 2021

Some gateway, routing nodes, such as firewalls will close the TCP connection that does not communicate for a long time(for example, 60 seconds), which leads to application communication failure. So we need to add a heartbeat to the application layer to keep the connection from being disconnected. In JS, setInterval() can be used to send some data to the server regularly through the connection, such as the following.

ws = new WebSocket('ws://example.com');
ws.onopen = function () {
    var t = setInterval(function(){
        if (ws.readyState != 1) {
            clearInterval(t);
            return;
        }
        ws.send('{type:"ping"}');
    }, 55000);
};

When the server receives heartbeat data, it can ignore it and do nothing. Heartbeat interval less than 60 seconds is better, such as 55 seconds.

@litosh-web
Copy link
Author

litosh-web commented Jan 21, 2021

May be It will help. But If I open my website(websocket) in the same browser with two similar tabs and start clicking (checking websocket work) on the first and on the second (and reload this) , then I'll get this error too. I changed interval for a 5 seconds and it didn't affect. WebSocket is already in CLOSING or CLOSED state

@walkor
Copy link
Owner

walkor commented Jan 22, 2021

WebSocket is already in CLOSING or CLOSED state means that the connection has been disconnected and data cannot be sent through this connection. In addition to the lack of heartbeat and being disconnected by the network node, it is also possible that the client or server called the close method, and there may be a bug in your business code.

@litosh-web
Copy link
Author

litosh-web commented Jan 22, 2021

The problem is not with connection anymore. The problem with multiple tabs. When 1 user open 15 tabs, then connection will lose. I found an answer on stackoverflow about this issue. Link https://stackoverflow.com/questions/59176767/socket-io-with-multiple-tabs-not-working-and-when-refreshing-the-page

I check connection variable and found out, that workerId has not unique id. And it possible affected on this error.

@walkor
Copy link
Owner

walkor commented Jan 26, 2021

Is the problem solved?

@litosh-web
Copy link
Author

litosh-web commented Jan 26, 2021

Yes, the problem was with my code (sql requests). Also your documentation is small

@EbiFeke
Copy link

EbiFeke commented Aug 24, 2022

please my webterm shows disconnected...terminado and goes on to show websocket closed, how do i fix this?

@litosh-web
Copy link
Author

please my webterm shows disconnected...terminado and goes on to show websocket closed, how do i fix this?

  1. Get sample from repository
  2. Don't use SESSION
  3. Check SQL queries
  4. Check logs errors

@Ambyzhale
Copy link

how may you help me who has just started a software engineering course and i still don't know how to write a "c" or "js" script

@Engedasew-Haile
Copy link

unable access git hub resources,. its said disconnected or closed
[WebSocket is already in CLOSING or CLOSED state.]
[WebSocket is already in CLOSING or CLOSED state.]

@Danny-coursera
Copy link

how do i get back to where i stopped when the web socket closes automatically?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants