close
Summary
Closes the WebSocket connection or connection attempt, if any.
Method of apis/websocket/WebSocketapis/websocket/WebSocket
Syntax
object.close(code, reason);
Parameters
code
- Data-type
- unsigned short
(Optional)
A numeric value indicating the status code explaining why the connection is being closed. If this parameter is not specified, a default value of 1000 (indicating a normal “transaction complete” closure) is assumed.
reason
- Data-type
- String
(Optional)
A human-readable string explaining why the connection is closing. This string must be no longer than 123 bytes of UTF-8 text (not characters).
Return Value
No return value
Notes
The onclose event will return three attributes:
- wasClean (binary) - whether the connection closed cleanly.
- code (unsigned long) - code from the server.
- reason (text) - reason provided by the server.
If the code parameter is present but is not an integer equal to 1000 or in the range 3000 to 4999, this method throws an InvalidAccessError exception and aborts. If the reason parameter is longer than 123 bytes this method throws a SyntaxError exception, and aborts.
Related specifications
- W3C WebSocket Specification
- W3C Candidate Recommendation
Attributions
Mozilla Developer Network : Article
Microsoft Developer Network: Windows Internet Explorer API reference Article