WebSocket.close() method closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED , this method does nothing.

句法

WebSocket.close();
					

参数

code 可选
A numeric value indicating the status code explaining why the connection is being closed. If this parameter is not specified, a default value of 1005 is assumed. See the list of status codes of CloseEvent for permitted values.
reason 可选
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).

Exceptions thrown

INVALID_ACCESS_ERR
An invalid code was specified.
SYNTAX_ERR
reason string is too long or contains unpaired surrogates.

注意: In Gecko, this method didn't support any parameters prior to Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5).

规范

规范 状态 注释
HTML 实时标准
The definition of 'WebSocket.close()' in that specification.
实时标准 初始定义

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
close Chrome 4 Edge 12 Firefox 8
8
不支持 4 — 8
Parameters not supported, see bug 674716 .
IE 10 Opera 12.1 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 8
8
不支持 4 — 8
Parameters not supported, see bug 674716 .
Opera Android 12.1 Safari iOS 4.2 Samsung Internet Android 1.0

图例

完整支持

完整支持

见实现注意事项。

元数据

  • 最后修改: