'Identify if Socket server is running from client side
When the socket server is not running, client-side java script codes creates a connection, and then socket.onopen creates a fetal error and the javascript compiler stops executing.
There is apparently no way to detect if socket server is running or not.
socket = new WebSocket(socket_serverurl);
In both cases, it returns, socket.readyState = 0. onerror is not triggering as well.
How do I detect the socket server is running or not and handle this error gracefully?
Solution 1:[1]
There is apparently no way to detect if socket server is running or not.
Not specifically, by design. Differentiating why the connection fails is a security risk, per the WebSocket interface spec:
https://websockets.spec.whatwg.org/#feedback-from-the-protocol
User agents must not convey any failure information to scripts in a way that would allow a script to distinguish the following situations:
...
In all of these cases, the WebSocket connection close code would be 1006, as required by WebSocket Protocol. [WSP]
Allowing a script to distinguish these cases would allow a script to probe the user’s local network in preparation for an attack.
However, as you stated it in your question:
socket.onopen creates a fetal error
Except that you got one detail wrong. The onopen event is not fired if the connection fails. The onclose event is fired instead, as stated in the WebSockets interface spec:
https://websockets.spec.whatwg.org/#eventdef-websocket-open
If the "establish a WebSocket connection" algorithm fails, it triggers the "fail the WebSocket connection" algorithm, which then invokes the "close the WebSocket connection" algorithm, which then establishes that "the WebSocket connection is closed", which fires the close event
This matches up with the WebSocket protocol spec:
https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.4
7.1.4. The WebSocket Connection is Closed
When the underlying TCP connection is closed, it is said that The WebSocket Connection is Closed and that the WebSocket connection is in the CLOSED state. If the TCP connection was closed after the WebSocket closing handshake was completed, the WebSocket connection is said to have been closed cleanly.
If the WebSocket connection could not be established, it is also said that The WebSocket Connection is Closed, but not cleanly.
ReadyState 0 is CONNECTING. So, if you get an onclose event in this state (where event.wasClean should be false and event.code should be 1006), you know the connection was unsuccessful.
In addition, per the interface spec, you should also get an onerror event, despite your claim that you are not. The interface spec is very clear on the client's responsibility on this matter:
https://websockets.spec.whatwg.org/#feedback-from-the-protocol
When the WebSocket connection is closed, possibly cleanly, the user agent must queue a task to run the following substeps:
- Change the ready state to
CLOSED(3).- If the user agent was required to fail the WebSocket connection, or if the WebSocket connection was closed" after being flagged as full, fire an event named
errorat the WebSocket object. [WSP]- Fire an event named
closeat the WebSocket object, usingCloseEvent, with thewasCleanattribute initialized to true if the connection closed cleanly and false otherwise, thecodeattribute initialized to the WebSocket connection close code, and thereasonattribute initialized to the result of applying UTF-8 decode without BOM to the WebSocket connection close reason. [WSP]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
