WebSocket.close()

翻译不完整。 请帮助我们翻译这篇文章!

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

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
closeChrome Full support 4Edge Full support 12Firefox Full support 8
Full support 8
No support 4 — 8
Notes
Notes Parameters not supported, see bug 674716.
IE Full support 10Opera Full support 12.1Safari Full support 5WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 8
Full support 8
No support 4 — 8
Notes
Notes Parameters not supported, see bug 674716.
Opera Android Full support 12.1Safari iOS Full support 4.2Samsung Internet Android Full support 1.0

Legend

Full support  
Full support
See implementation notes.
See implementation notes.