Changeset e29cc2e for trip-planner-front/node_modules/ws/README.md
- Timestamp:
- 11/25/21 22:08:24 (3 years ago)
- Branches:
- master
- Children:
- 8d391a1
- Parents:
- 59329aa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner-front/node_modules/ws/README.md
r59329aa re29cc2e 2 2 3 3 [![Version npm](https://img.shields.io/npm/v/ws.svg?logo=npm)](https://www.npmjs.com/package/ws) 4 [![ Build](https://img.shields.io/github/workflow/status/websockets/ws/CI/master?label=build&logo=github)](https://github.com/websockets/ws/actions?query=workflow%3ACI+branch%3Amaster)5 [![Windows x86Build](https://img.shields.io/appveyor/ci/lpinca/ws/master.svg?logo=appveyor)](https://ci.appveyor.com/project/lpinca/ws)4 [![Linux Build](https://img.shields.io/travis/websockets/ws/master.svg?logo=travis)](https://travis-ci.org/websockets/ws) 5 [![Windows Build](https://img.shields.io/appveyor/ci/lpinca/ws/master.svg?logo=appveyor)](https://ci.appveyor.com/project/lpinca/ws) 6 6 [![Coverage Status](https://img.shields.io/coveralls/websockets/ws/master.svg)](https://coveralls.io/github/websockets/ws) 7 7 … … 33 33 - [External HTTP/S server](#external-https-server) 34 34 - [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server) 35 - [Client authentication](#client-authentication)36 35 - [Server broadcast](#server-broadcast) 37 36 - [echo.websocket.org demo](#echowebsocketorg-demo) 38 - [Use the Node.js streams API](#use-the-nodejs-streams-api)39 37 - [Other examples](#other-examples) 38 - [Error handling best practices](#error-handling-best-practices) 40 39 - [FAQ](#faq) 41 40 - [How to get the IP address of the client?](#how-to-get-the-ip-address-of-the-client) … … 57 56 ``` 58 57 59 ### Opt-in for performance 58 ### Opt-in for performance and spec compliance 60 59 61 60 There are 2 optional modules that can be installed along side with the ws … … 68 67 frames. 69 68 - `npm install --save-optional utf-8-validate`: Allows to efficiently check if a 70 message contains valid UTF-8 .69 message contains valid UTF-8 as required by the spec. 71 70 72 71 ## API docs 73 72 74 See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and 75 utility functions. 73 See [`/doc/ws.md`](./doc/ws.md) for Node.js-like docs for the ws classes. 76 74 77 75 ## WebSocket compression … … 196 194 const WebSocket = require('ws'); 197 195 198 const server = https.createServer({196 const server = new https.createServer({ 199 197 cert: fs.readFileSync('/path/to/cert.pem'), 200 198 key: fs.readFileSync('/path/to/key.pem') … … 218 216 const http = require('http'); 219 217 const WebSocket = require('ws'); 220 const url = require('url');221 218 222 219 const server = http.createServer(); … … 251 248 ``` 252 249 253 ### Client authentication 254 255 ```js 256 const http = require('http'); 257 const WebSocket = require('ws'); 258 259 const server = http.createServer(); 260 const wss = new WebSocket.Server({ noServer: true }); 261 262 wss.on('connection', function connection(ws, request, client) { 263 ws.on('message', function message(msg) { 264 console.log(`Received message ${msg} from user ${client}`); 250 ### Server broadcast 251 252 ```js 253 const WebSocket = require('ws'); 254 255 const wss = new WebSocket.Server({ port: 8080 }); 256 257 // Broadcast to all. 258 wss.broadcast = function broadcast(data) { 259 wss.clients.forEach(function each(client) { 260 if (client.readyState === WebSocket.OPEN) { 261 client.send(data); 262 } 265 263 }); 266 }); 267 268 server.on('upgrade', function upgrade(request, socket, head) { 269 // This function is not defined on purpose. Implement it with your own logic. 270 authenticate(request, (err, client) => { 271 if (err || !client) { 272 socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n'); 273 socket.destroy(); 274 return; 275 } 276 277 wss.handleUpgrade(request, socket, head, function done(ws) { 278 wss.emit('connection', ws, request, client); 279 }); 280 }); 281 }); 282 283 server.listen(8080); 284 ``` 285 286 Also see the provided [example][session-parse-example] using `express-session`. 287 288 ### Server broadcast 289 290 A client WebSocket broadcasting to all connected WebSocket clients, including 291 itself. 292 293 ```js 294 const WebSocket = require('ws'); 295 296 const wss = new WebSocket.Server({ port: 8080 }); 264 }; 297 265 298 266 wss.on('connection', function connection(ws) { 299 267 ws.on('message', function incoming(data) { 300 wss.clients.forEach(function each(client) { 301 if (client.readyState === WebSocket.OPEN) { 302 client.send(data); 303 } 304 }); 305 }); 306 }); 307 ``` 308 309 A client WebSocket broadcasting to every other connected WebSocket clients, 310 excluding itself. 311 312 ```js 313 const WebSocket = require('ws'); 314 315 const wss = new WebSocket.Server({ port: 8080 }); 316 317 wss.on('connection', function connection(ws) { 318 ws.on('message', function incoming(data) { 268 // Broadcast to everyone else. 319 269 wss.clients.forEach(function each(client) { 320 270 if (client !== ws && client.readyState === WebSocket.OPEN) { … … 353 303 ``` 354 304 355 ### Use the Node.js streams API356 357 ```js358 const WebSocket = require('ws');359 360 const ws = new WebSocket('wss://echo.websocket.org/', {361 origin: 'https://websocket.org'362 });363 364 const duplex = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' });365 366 duplex.pipe(process.stdout);367 process.stdin.pipe(duplex);368 ```369 370 305 ### Other examples 371 306 … … 375 310 Otherwise, see the test cases. 376 311 312 ## Error handling best practices 313 314 ```js 315 // If the WebSocket is closed before the following send is attempted 316 ws.send('something'); 317 318 // Errors (both immediate and async write errors) can be detected in an optional 319 // callback. The callback is also the only way of being notified that data has 320 // actually been sent. 321 ws.send('something', function ack(error) { 322 // If error is not defined, the send has been completed, otherwise the error 323 // object will indicate what failed. 324 }); 325 326 // Immediate errors can also be handled with `try...catch`, but **note** that 327 // since sends are inherently asynchronous, socket write failures will *not* be 328 // captured when this technique is used. 329 try { 330 ws.send('something'); 331 } catch (e) { 332 /* handle error */ 333 } 334 ``` 335 377 336 ## FAQ 378 337 … … 387 346 388 347 wss.on('connection', function connection(ws, req) { 389 const ip = req. socket.remoteAddress;348 const ip = req.connection.remoteAddress; 390 349 }); 391 350 ``` … … 433 392 }); 434 393 }, 30000); 435 436 wss.on('close', function close() {437 clearInterval(interval);438 });439 394 ``` 440 395 … … 452 407 clearTimeout(this.pingTimeout); 453 408 454 // Use `WebSocket#terminate()`, which immediately destroys the connection, 455 // instead of `WebSocket#close()`, which waits for the close timer. 456 // Delay should be equal to the interval at which your server 457 // sends out pings plus a conservative assumption of the latency. 409 // Use `WebSocket#terminate()` and not `WebSocket#close()`. Delay should be 410 // equal to the interval at which your server sends out pings plus a 411 // conservative assumption of the latency. 458 412 this.pingTimeout = setTimeout(() => { 459 413 this.terminate(); … … 483 437 [MIT](LICENSE) 484 438 439 [https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent 440 [socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent 441 [client-report]: http://websockets.github.io/ws/autobahn/clients/ 442 [server-report]: http://websockets.github.io/ws/autobahn/servers/ 443 [permessage-deflate]: https://tools.ietf.org/html/rfc7692 485 444 [changelog]: https://github.com/websockets/ws/releases 486 [client-report]: http://websockets.github.io/ws/autobahn/clients/487 [https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent488 445 [node-zlib-bug]: https://github.com/nodejs/node/issues/8871 489 446 [node-zlib-deflaterawdocs]: 490 447 https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options 491 [permessage-deflate]: https://tools.ietf.org/html/rfc7692492 [server-report]: http://websockets.github.io/ws/autobahn/servers/493 [session-parse-example]: ./examples/express-session-parse494 [socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent495 448 [ws-server-options]: 496 449 https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
Note:
See TracChangeset
for help on using the changeset viewer.