WebSocket()
构造函器会返回一个 WebSocket
对象。
语法
var aWebSocket = new WebSocket(url [, protocols]);
参数
url
- 要连接的URL;这应该是WebSocket服务器将响应的URL。
protocols
可选- 一个协议字符串或者一个包含协议字符串的数组。这些字符串用于指定子协议,这样单个服务器可以实现多个WebSocket子协议(例如,您可能希望一台服务器能够根据指定的协议(
protocol
)处理不同类型的交互)。如果不指定协议字符串,则假定为空字符串。
抛出异常
SECURITY_ERR
- 正在尝试连接的端口被阻止。
规范
规范 | 状态 | 注释 |
---|---|---|
HTML Living Standard the WebSocket constructor |
Living Standard |
浏览器兼容性
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.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
WebSocket() constructor | Chrome Full support Yes | Edge ? | Firefox
Full support
7
| IE ? | Opera Full support Yes | Safari ? | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 7 | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.