WebSocket.close()
方法关闭 WebSocket
连接或连接尝试(如果有的话)。 如果连接已经关闭,则此方法不执行任何操作。
语法
WebSocket.close();
参数
code
可选- 一个数字状态码,它解释了连接关闭的原因。如果没有传这个参数,默认使用1005。
CloseEvent
的允许的状态码见状态码列表 。 reason
可选- 一个人类可读的字符串,它解释了连接关闭的原因。这个UTF-8编码的字符串不能超过123个字节。
抛出的异常
INVALID_ACCESS_ERR
- 一个无效的
code
SYNTAX_ERR
reason
字符串太长(超过123字节)
注意: 在Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5)之前版本的Gecko里,这个方法不支持传参数。
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'WebSocket.close()' in that specification. |
Living Standard | Initial definition |
Browser compatibility
BCD tables only load in the browser
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.