main
Last change
on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Line | |
---|
1 | # Class: WebSocket
|
---|
2 |
|
---|
3 | > ⚠️ Warning: the WebSocket API is experimental.
|
---|
4 |
|
---|
5 | Extends: [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)
|
---|
6 |
|
---|
7 | The WebSocket object provides a way to manage a WebSocket connection to a server, allowing bidirectional communication. The API follows the [WebSocket spec](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) and [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455).
|
---|
8 |
|
---|
9 | ## `new WebSocket(url[, protocol])`
|
---|
10 |
|
---|
11 | Arguments:
|
---|
12 |
|
---|
13 | * **url** `URL | string` - The url's protocol *must* be `ws` or `wss`.
|
---|
14 | * **protocol** `string | string[] | WebSocketInit` (optional) - Subprotocol(s) to request the server use, or a [`Dispatcher`](./Dispatcher.md).
|
---|
15 |
|
---|
16 | ### Example:
|
---|
17 |
|
---|
18 | This example will not work in browsers or other platforms that don't allow passing an object.
|
---|
19 |
|
---|
20 | ```mjs
|
---|
21 | import { WebSocket, ProxyAgent } from 'undici'
|
---|
22 |
|
---|
23 | const proxyAgent = new ProxyAgent('my.proxy.server')
|
---|
24 |
|
---|
25 | const ws = new WebSocket('wss://echo.websocket.events', {
|
---|
26 | dispatcher: proxyAgent,
|
---|
27 | protocols: ['echo', 'chat']
|
---|
28 | })
|
---|
29 | ```
|
---|
30 |
|
---|
31 | If you do not need a custom Dispatcher, it's recommended to use the following pattern:
|
---|
32 |
|
---|
33 | ```mjs
|
---|
34 | import { WebSocket } from 'undici'
|
---|
35 |
|
---|
36 | const ws = new WebSocket('wss://echo.websocket.events', ['echo', 'chat'])
|
---|
37 | ```
|
---|
38 |
|
---|
39 | ## Read More
|
---|
40 |
|
---|
41 | - [MDN - WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)
|
---|
42 | - [The WebSocket Specification](https://www.rfc-editor.org/rfc/rfc6455)
|
---|
43 | - [The WHATWG WebSocket Specification](https://websockets.spec.whatwg.org/)
|
---|
Note:
See
TracBrowser
for help on using the repository browser.