WebSocket.close()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
WebSocket.close()
方法关闭 WebSocket
连接或连接尝试(如果有的话)。如果连接已经关闭,则此方法不执行任何操作。
语法
js
close()
close(code)
close(code, reason)
参数
code
可选-
一个数字状态码,它解释了连接关闭的原因。如果没有传这个参数,默认使用 1005。
CloseEvent
的允许的状态码见状态码列表 。 reason
可选-
一个人类可读的字符串,它解释了连接关闭的原因。这个 UTF-8 编码的字符串不能超过 123 个字节。
抛出的异常
INVALID_ACCESS_ERR
-
一个无效的
code
SYNTAX_ERR
-
reason
字符串太长(超过 123 字节)
Specifications
Specification |
---|
WebSockets # ref-for-dom-websocket-close① |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
close |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.