WebSocket() constructor returns a new WebSocket 对象。

句法

var aWebSocket = new WebSocket(url [, protocols]);
					

参数

url

The URL to which to connect; this should be the URL to which the WebSocket server will respond.

protocols 可选
Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols, so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to be able to handle different types of interactions depending on the specified 协议 ). If you don't specify a protocol string, an empty string is assumed.

Exceptions thrown

SECURITY_ERR

The port to which the connection is being attempted is being blocked.

SyntaxError

The URL is invalid.

规范

规范 状态 注释
HTML 实时标准
The definition of 'the WebSocket constructor' 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
WebSocket() 构造函数 Chrome Yes Edge ≤79 Firefox 7
7
不支持 4 — 7
参数 protocols not supported.
IE ? Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android 7 Opera Android ? Safari iOS ? Samsung Internet Android Yes

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

见实现注意事项。

元数据

  • 最后修改: