readyState
Summary
The current state of the connection, represented as a numeric constant.
Property of apis/websocket/WebSocketapis/websocket/WebSocket
Syntax
Note: This property is read-only.
var result = element.readyState;
Return Value
Returns an object of type unsigned shortunsigned short
These constants are used by the readyState attribute to describe the state of the WebSocket connection.
Constant | Value | Description |
---|---|---|
CONNECTING | 0 | The connection is not yet open. |
OPEN | 1 | The connection is open and ready to communicate. |
CLOSING | 2 | The connection is in the process of closing. |
CLOSED | 3 | The connection is closed or couldn’t be opened. |
Examples
switch (socket.readyState) {
case WebSocket.CONNECTING:
// do something
break;
case WebSocket.OPEN:
// do something
break;
case WebSocket.CLOSING:
// do something
break;
case WebSocket.CLOSED:
// do something
break;
default:
// this never happens
break;
}
Related specifications
- W3C WebSocket Specification
- W3C Candidate Recommendation
Attributions
Mozilla Developer Network : Article
Microsoft Developer Network: Windows Internet Explorer API reference Article