Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i know if connection is alive with websockets?

Tags:

I have a webapp, which is running in a browser. That webapp is connected to a server, which uses websockets. So the communication between the server and my client/browser is based on websockets. If some magic event occurs on the server, some webservice sends a new XML / JSON to my webapp and the new data gets displayed.

But how do i, as the client / browser, know if the connection is stil alive? Lets say i do not get any new XML for about 30 seconds. How would i know if the connection is closed/broken/server offline or everything is fine, but on the server himself no new magic event occured.

like image 429
Gero Avatar asked Aug 04 '12 14:08

Gero


People also ask

How do I check my WebSocket connection status?

In the search field, enter websocket . From the search results, click WebSocket Connection Status.

How long is a WebSocket connection alive?

Keepalive in websockets Wait 20 seconds. Send a Ping frame.

Is WebSocket keep alive?

WebSocket is a bidirectional communication protocol that can send the data from the client to the server or from the server to the client by reusing the established connection channel. The connection is kept alive until terminated by either the client or the server.


1 Answers

A websocket connection object has a readyState field which will tell you if the connection is still active (from the dart documentation). The readyState can be either

0 - connection not yet established 1 - conncetion established 2 - in closing handshake 3 - connection closed or could not open 

You can also define an event handler for the websocket close event if this is something you'd like to handle (try to reconnect, etc).

like image 192
fuzic Avatar answered Sep 21 '22 17:09

fuzic