1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
# ws
## Table of Contents
- [Class: WebSocket.Server](#class-websocketserver)
- [new WebSocket.Server(options[, callback])](#new-websocketserveroptions-callback)
- [Event: 'close'](#event-close)
- [Event: 'connection'](#event-connection)
- [Event: 'error'](#event-error)
- [Event: 'headers'](#event-headers)
- [Event: 'listening'](#event-listening)
- [server.address()](#serveraddress)
- [server.clients](#serverclients)
- [server.close([callback])](#serverclosecallback)
- [server.handleUpgrade(request, socket, head, callback)](#serverhandleupgraderequest-socket-head-callback)
- [server.shouldHandle(request)](#servershouldhandlerequest)
- [Class: WebSocket](#class-websocket)
- [Ready state constants](#ready-state-constants)
- [new WebSocket(address[, protocols][, options])](#new-websocketaddress-protocols-options)
- [UNIX Domain Sockets](#unix-domain-sockets)
- [Event: 'close'](#event-close-1)
- [Event: 'error'](#event-error-1)
- [Event: 'message'](#event-message)
- [Event: 'open'](#event-open)
- [Event: 'ping'](#event-ping)
- [Event: 'pong'](#event-pong)
- [Event: 'unexpected-response'](#event-unexpected-response)
- [Event: 'upgrade'](#event-upgrade)
- [websocket.addEventListener(type, listener[, options])](#websocketaddeventlistenertype-listener-options)
- [websocket.binaryType](#websocketbinarytype)
- [websocket.bufferedAmount](#websocketbufferedamount)
- [websocket.close([code[, reason]])](#websocketclosecode-reason)
- [websocket.extensions](#websocketextensions)
- [websocket.onclose](#websocketonclose)
- [websocket.onerror](#websocketonerror)
- [websocket.onmessage](#websocketonmessage)
- [websocket.onopen](#websocketonopen)
- [websocket.ping([data[, mask]][, callback])](#websocketpingdata-mask-callback)
- [websocket.pong([data[, mask]][, callback])](#websocketpongdata-mask-callback)
- [websocket.protocol](#websocketprotocol)
- [websocket.readyState](#websocketreadystate)
- [websocket.removeEventListener(type, listener)](#websocketremoveeventlistenertype-listener)
- [websocket.send(data[, options][, callback])](#websocketsenddata-options-callback)
- [websocket.terminate()](#websocketterminate)
- [websocket.url](#websocketurl)
- [WebSocket.createWebSocketStream(websocket[, options])](#websocketcreatewebsocketstreamwebsocket-options)
## Class: WebSocket.Server
This class represents a WebSocket server. It extends the `EventEmitter`.
### new WebSocket.Server(options[, callback])
- `options` {Object}
- `host` {String} The hostname where to bind the server.
- `port` {Number} The port where to bind the server.
- `backlog` {Number} The maximum length of the queue of pending connections.
- `server` {http.Server|https.Server} A pre-created Node.js HTTP/S server.
- `verifyClient` {Function} A function which can be used to validate incoming
connections. See description below. (Usage is discouraged: see
[Issue #337](https://github.com/websockets/ws/issues/377#issuecomment-462152231))
- `handleProtocols` {Function} A function which can be used to handle the
WebSocket subprotocols. See description below.
- `path` {String} Accept only connections matching this path.
- `noServer` {Boolean} Enable no server mode.
- `clientTracking` {Boolean} Specifies whether or not to track clients.
- `perMessageDeflate` {Boolean|Object} Enable/disable permessage-deflate.
- `maxPayload` {Number} The maximum allowed message size in bytes.
- `callback` {Function}
Create a new server instance. One of `port`, `server` or `noServer` must be
provided or an error is thrown. An HTTP server is automatically created,
started, and used if `port` is set. To use an external HTTP/S server instead,
specify only `server` or `noServer`. In this case the HTTP/S server must be
started manually. The "noServer" mode allows the WebSocket server to be
completly detached from the HTTP/S server. This makes it possible, for example,
to share a single HTTP/S server between multiple WebSocket servers.
> **NOTE:** Use of `verifyClient` is discouraged. Rather handle client
> authentication in the `upgrade` event of the HTTP server. See examples for
> more details.
If `verifyClient` is not set then the handshake is automatically accepted. If it
is provided with a single argument then that is:
- `info` {Object}
- `origin` {String} The value in the Origin header indicated by the client.
- `req` {http.IncomingMessage} The client HTTP GET request.
- `secure` {Boolean} `true` if `req.connection.authorized` or
`req.connection.encrypted` is set.
The return value (`Boolean`) of the function determines whether or not to accept
the handshake.
if `verifyClient` is provided with two arguments then those are:
- `info` {Object} Same as above.
- `cb` {Function} A callback that must be called by the user upon inspection of
the `info` fields. Arguments in this callback are:
- `result` {Boolean} Whether or not to accept the handshake.
- `code` {Number} When `result` is `false` this field determines the HTTP
error status code to be sent to the client.
- `name` {String} When `result` is `false` this field determines the HTTP
reason phrase.
- `headers` {Object} When `result` is `false` this field determines additional
HTTP headers to be sent to the client. For example,
`{ 'Retry-After': 120 }`.
`handleProtocols` takes two arguments:
- `protocols` {Array} The list of WebSocket subprotocols indicated by the client
in the `Sec-WebSocket-Protocol` header.
- `request` {http.IncomingMessage} The client HTTP GET request.
The returned value sets the value of the `Sec-WebSocket-Protocol` header in the
HTTP 101 response. If returned value is `false` the header is not added in the
response.
If `handleProtocols` is not set then the first of the client's requested
subprotocols is used.
`perMessageDeflate` can be used to control the behavior of [permessage-deflate
extension][permessage-deflate]. The extension is disabled when `false` (default
value). If an object is provided then that is extension parameters:
- `serverNoContextTakeover` {Boolean} Whether to use context takeover or not.
- `clientNoContextTakeover` {Boolean} Acknowledge disabling of client context
takeover.
- `serverMaxWindowBits` {Number} The value of `windowBits`.
- `clientMaxWindowBits` {Number} Request a custom client window size.
- `zlibDeflateOptions` {Object} [Additional options][zlib-options] to pass to
zlib on deflate.
- `zlibInflateOptions` {Object} [Additional options][zlib-options] to pass to
zlib on inflate.
- `threshold` {Number} Payloads smaller than this will not be compressed.
Defaults to 1024 bytes.
- `concurrencyLimit` {Number} The number of concurrent calls to zlib. Calls
above this limit will be queued. Default 10. You usually won't need to touch
this option. See [this issue][concurrency-limit] for more details.
If a property is empty then either an offered configuration or a default value
is used. When sending a fragmented message the length of the first fragment is
compared to the threshold. This determines if compression is used for the entire
message.
`callback` will be added as a listener for the `listening` event on the HTTP
server when not operating in "noServer" mode.
### Event: 'close'
Emitted when the server closes. This event depends on the `'close'` event of
HTTP server only when it is created internally. In all other cases, the event is
emitted independently.
### Event: 'connection'
- `websocket` {WebSocket}
- `request` {http.IncomingMessage}
Emitted when the handshake is complete. `request` is the http GET request sent
by the client. Useful for parsing authority headers, cookie headers, and other
information.
### Event: 'error'
- `error` {Error}
Emitted when an error occurs on the underlying server.
### Event: 'headers'
- `headers` {Array}
- `request` {http.IncomingMessage}
Emitted before the response headers are written to the socket as part of the
handshake. This allows you to inspect/modify the headers before they are sent.
### Event: 'listening'
Emitted when the underlying server has been bound.
### server.address()
Returns an object with `port`, `family`, and `address` properties specifying the
bound address, the address family name, and port of the server as reported by
the operating system if listening on an IP socket. If the server is listening on
a pipe or UNIX domain socket, the name is returned as a string.
### server.clients
- {Set}
A set that stores all connected clients. Please note that this property is only
added when the `clientTracking` is truthy.
### server.close([callback])
Close the HTTP server if created internally, terminate all clients and call
callback when done. If an external HTTP server is used via the `server` or
`noServer` constructor options, it must be closed manually.
### server.handleUpgrade(request, socket, head, callback)
- `request` {http.IncomingMessage} The client HTTP GET request.
- `socket` {net.Socket} The network socket between the server and client.
- `head` {Buffer} The first packet of the upgraded stream.
- `callback` {Function}.
Handle a HTTP upgrade request. When the HTTP server is created internally or
when the HTTP server is passed via the `server` option, this method is called
automatically. When operating in "noServer" mode, this method must be called
manually.
If the upgrade is successful, the `callback` is called with two arguments:
- `websocket` {WebSocket} A `WebSocket` object.
- `request` {http.IncomingMessage} The client HTTP GET request.
### server.shouldHandle(request)
- `request` {http.IncomingMessage} The client HTTP GET request.
See if a given request should be handled by this server. By default this method
validates the pathname of the request, matching it against the `path` option if
provided. The return value, `true` or `false`, determines whether or not to
accept the handshake.
This method can be overridden when a custom handling logic is required.
## Class: WebSocket
This class represents a WebSocket. It extends the `EventEmitter`.
### Ready state constants
| Constant | Value | Description |
| ---------- | ----- | ------------------------------------------------ |
| CONNECTING | 0 | The connection is not yet open. |
| OPEN | 1 | The connection is open and ready to communicate. |
| CLOSING | 2 | The connection is in the process of closing. |
| CLOSED | 3 | The connection is closed. |
### new WebSocket(address[, protocols][, options])
- `address` {String|url.URL} The URL to which to connect.
- `protocols` {String|Array} The list of subprotocols.
- `options` {Object}
- `followRedirects` {Boolean} Whether or not to follow redirects. Defaults to
`false`.
- `handshakeTimeout` {Number} Timeout in milliseconds for the handshake
request. This is reset after every redirection.
- `maxRedirects` {Number} The maximum number of redirects allowed. Defaults
to 10.
- `perMessageDeflate` {Boolean|Object} Enable/disable permessage-deflate.
- `protocolVersion` {Number} Value of the `Sec-WebSocket-Version` header.
- `origin` {String} Value of the `Origin` or `Sec-WebSocket-Origin` header
depending on the `protocolVersion`.
- `maxPayload` {Number} The maximum allowed message size in bytes.
- Any other option allowed in [http.request()][] or [https.request()][].
Options given do not have any effect if parsed from the URL given with the
`address` parameter.
`perMessageDeflate` default value is `true`. When using an object, parameters
are the same of the server. The only difference is the direction of requests.
For example, `serverNoContextTakeover` can be used to ask the server to disable
context takeover.
Create a new WebSocket instance.
#### UNIX Domain Sockets
`ws` supports making requests to UNIX domain sockets. To make one, use the
following URL scheme:
```
ws+unix:///absolute/path/to/uds_socket:/pathname?search_params
```
Note that `:` is the separator between the socket path and the URL path. If the
URL path is omitted
```
ws+unix:///absolute/path/to/uds_socket
```
it defaults to `/`.
### Event: 'close'
- `code` {Number}
- `reason` {String}
Emitted when the connection is closed. `code` is a numeric value indicating the
status code explaining why the connection has been closed. `reason` is a
human-readable string explaining why the connection has been closed.
### Event: 'error'
- `error` {Error}
Emitted when an error occurs.
### Event: 'message'
- `data` {String|Buffer|ArrayBuffer|Buffer[]}
Emitted when a message is received from the server.
### Event: 'open'
Emitted when the connection is established.
### Event: 'ping'
- `data` {Buffer}
Emitted when a ping is received from the server.
### Event: 'pong'
- `data` {Buffer}
Emitted when a pong is received from the server.
### Event: 'unexpected-response'
- `request` {http.ClientRequest}
- `response` {http.IncomingMessage}
Emitted when the server response is not the expected one, for example a 401
response. This event gives the ability to read the response in order to extract
useful information. If the server sends an invalid response and there isn't a
listener for this event, an error is emitted.
### Event: 'upgrade'
- `response` {http.IncomingMessage}
Emitted when response headers are received from the server as part of the
handshake. This allows you to read headers from the server, for example
'set-cookie' headers.
### websocket.addEventListener(type, listener[, options])
- `type` {String} A string representing the event type to listen for.
- `listener` {Function} The listener to add.
- `options` {Object}
- `once` {Boolean} A `Boolean` indicating that the listener should be invoked
at most once after being added. If `true`, the listener would be
automatically removed when invoked.
Register an event listener emulating the `EventTarget` interface.
### websocket.binaryType
- {String}
A string indicating the type of binary data being transmitted by the connection.
This should be one of "nodebuffer", "arraybuffer" or "fragments". Defaults to
"nodebuffer". Type "fragments" will emit the array of fragments as received from
the sender, without copyfull concatenation, which is useful for the performance
of binary protocols transferring large messages with multiple fragments.
### websocket.bufferedAmount
- {Number}
The number of bytes of data that have been queued using calls to `send()` but
not yet transmitted to the network. This deviates from the HTML standard in the
following ways:
1. If the data is immediately sent the value is `0`.
1. All framing bytes are included.
### websocket.close([code[, reason]])
- `code` {Number} A numeric value indicating the status code explaining why the
connection is being closed.
- `reason` {String} A human-readable string explaining why the connection is
closing.
Initiate a closing handshake.
### websocket.extensions
- {Object}
An object containing the negotiated extensions.
### websocket.onclose
- {Function}
An event listener to be called when connection is closed. The listener receives
a `CloseEvent` named "close".
### websocket.onerror
- {Function}
An event listener to be called when an error occurs. The listener receives an
`ErrorEvent` named "error".
### websocket.onmessage
- {Function}
An event listener to be called when a message is received from the server. The
listener receives a `MessageEvent` named "message".
### websocket.onopen
- {Function}
An event listener to be called when the connection is established. The listener
receives an `OpenEvent` named "open".
### websocket.ping([data[, mask]][, callback])
- `data` {Any} The data to send in the ping frame.
- `mask` {Boolean} Specifies whether `data` should be masked or not. Defaults to
`true` when `websocket` is not a server client.
- `callback` {Function} An optional callback which is invoked when the ping
frame is written out.
Send a ping.
### websocket.pong([data[, mask]][, callback])
- `data` {Any} The data to send in the pong frame.
- `mask` {Boolean} Specifies whether `data` should be masked or not. Defaults to
`true` when `websocket` is not a server client.
- `callback` {Function} An optional callback which is invoked when the pong
frame is written out.
Send a pong.
### websocket.protocol
- {String}
The subprotocol selected by the server.
### websocket.readyState
- {Number}
The current state of the connection. This is one of the ready state constants.
### websocket.removeEventListener(type, listener)
- `type` {String} A string representing the event type to remove.
- `listener` {Function} The listener to remove.
Removes an event listener emulating the `EventTarget` interface.
### websocket.send(data[, options][, callback])
- `data` {Any} The data to send.
- `options` {Object}
- `compress` {Boolean} Specifies whether `data` should be compressed or not.
Defaults to `true` when permessage-deflate is enabled.
- `binary` {Boolean} Specifies whether `data` should be sent as a binary or
not. Default is autodetected.
- `mask` {Boolean} Specifies whether `data` should be masked or not. Defaults
to `true` when `websocket` is not a server client.
- `fin` {Boolean} Specifies whether `data` is the last fragment of a message
or not. Defaults to `true`.
- `callback` {Function} An optional callback which is invoked when `data` is
written out.
Send `data` through the connection.
### websocket.terminate()
Forcibly close the connection.
### websocket.url
- {String}
The URL of the WebSocket server. Server clients don't have this attribute.
## WebSocket.createWebSocketStream(websocket[, options])
- `websocket` {WebSocket} A `WebSocket` object.
- `options` {Object} [Options][duplex-options] to pass to the `Duplex`
constructor.
Returns a `Duplex` stream that allows to use the Node.js streams API on top of a
given `WebSocket`.
[concurrency-limit]: https://github.com/websockets/ws/issues/1202
[duplex-options]:
https://nodejs.org/api/stream.html#stream_new_stream_duplex_options
[http.request()]:
https://nodejs.org/api/http.html#http_http_request_options_callback
[https.request()]:
https://nodejs.org/api/https.html#https_https_request_options_callback
[permessage-deflate]:
https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-19
[zlib-options]: https://nodejs.org/api/zlib.html#zlib_class_options
|