@@ -98,9 +98,9 @@ into the creation of [raw deflate/inflate streams][node-zlib-deflaterawdocs].
98
98
See [ the docs] [ ws-server-options ] for more options.
99
99
100
100
``` js
101
- const WebSocket = require ( ' ws' ) ;
101
+ import WebSocket , { WebSocketServer } from ' ws' ;
102
102
103
- const wss = new WebSocket.Server ({
103
+ const wss = new WebSocketServer ({
104
104
port: 8080 ,
105
105
perMessageDeflate: {
106
106
zlibDeflateOptions: {
@@ -129,7 +129,7 @@ server. To always disable the extension on the client set the
129
129
` perMessageDeflate ` option to ` false ` .
130
130
131
131
``` js
132
- const WebSocket = require ( ' ws' ) ;
132
+ import WebSocket from ' ws' ;
133
133
134
134
const ws = new WebSocket (' ws://www.host.com/path' , {
135
135
perMessageDeflate: false
@@ -141,7 +141,7 @@ const ws = new WebSocket('ws://www.host.com/path', {
141
141
### Sending and receiving text data
142
142
143
143
``` js
144
- const WebSocket = require ( ' ws' ) ;
144
+ import WebSocket from ' ws' ;
145
145
146
146
const ws = new WebSocket (' ws://www.host.com/path' );
147
147
@@ -157,7 +157,7 @@ ws.on('message', function incoming(message) {
157
157
### Sending binary data
158
158
159
159
``` js
160
- const WebSocket = require ( ' ws' ) ;
160
+ import WebSocket from ' ws' ;
161
161
162
162
const ws = new WebSocket (' ws://www.host.com/path' );
163
163
@@ -175,9 +175,9 @@ ws.on('open', function open() {
175
175
### Simple server
176
176
177
177
``` js
178
- const WebSocket = require ( ' ws' ) ;
178
+ import { WebSocketServer } from ' ws' ;
179
179
180
- const wss = new WebSocket.Server ({ port: 8080 });
180
+ const wss = new WebSocketServer ({ port: 8080 });
181
181
182
182
wss .on (' connection' , function connection (ws ) {
183
183
ws .on (' message' , function incoming (message ) {
@@ -191,15 +191,15 @@ wss.on('connection', function connection(ws) {
191
191
### External HTTP/S server
192
192
193
193
``` js
194
- const fs = require ( ' fs ' ) ;
195
- const https = require ( ' https ' ) ;
196
- const WebSocket = require ( ' ws' ) ;
194
+ import { createServer } from ' https ' ;
195
+ import { readFileSync } from ' fs ' ;
196
+ import { WebSocketServer } from ' ws' ;
197
197
198
- const server = https . createServer ({
199
- cert: fs . readFileSync (' /path/to/cert.pem' ),
200
- key: fs . readFileSync (' /path/to/key.pem' )
198
+ const server = createServer ({
199
+ cert: readFileSync (' /path/to/cert.pem' ),
200
+ key: readFileSync (' /path/to/key.pem' )
201
201
});
202
- const wss = new WebSocket.Server ({ server });
202
+ const wss = new WebSocketServer ({ server });
203
203
204
204
wss .on (' connection' , function connection (ws ) {
205
205
ws .on (' message' , function incoming (message ) {
@@ -215,13 +215,13 @@ server.listen(8080);
215
215
### Multiple servers sharing a single HTTP/S server
216
216
217
217
``` js
218
- const http = require ( ' http' ) ;
219
- const WebSocket = require ( ' ws ' ) ;
220
- const url = require ( ' url ' ) ;
218
+ import { createServer } from ' http' ;
219
+ import { parse } from ' url ' ;
220
+ import { WebSocketServer } from ' ws ' ;
221
221
222
- const server = http . createServer ();
223
- const wss1 = new WebSocket.Server ({ noServer: true });
224
- const wss2 = new WebSocket.Server ({ noServer: true });
222
+ const server = createServer ();
223
+ const wss1 = new WebSocketServer ({ noServer: true });
224
+ const wss2 = new WebSocketServer ({ noServer: true });
225
225
226
226
wss1 .on (' connection' , function connection (ws ) {
227
227
// ...
@@ -232,7 +232,7 @@ wss2.on('connection', function connection(ws) {
232
232
});
233
233
234
234
server .on (' upgrade' , function upgrade (request , socket , head ) {
235
- const pathname = url . parse (request .url ). pathname ;
235
+ const { pathname } = parse (request .url );
236
236
237
237
if (pathname === ' /foo' ) {
238
238
wss1 .handleUpgrade (request, socket, head, function done (ws ) {
@@ -253,11 +253,11 @@ server.listen(8080);
253
253
### Client authentication
254
254
255
255
``` js
256
- const http = require ( ' http ' ) ;
257
- const WebSocket = require ( ' ws ' ) ;
256
+ import WebSocket from ' ws ' ;
257
+ import { createServer } from ' http ' ;
258
258
259
- const server = http . createServer ();
260
- const wss = new WebSocket.Server ({ noServer: true });
259
+ const server = createServer ();
260
+ const wss = new WebSocketServer ({ noServer: true });
261
261
262
262
wss .on (' connection' , function connection (ws , request , client ) {
263
263
ws .on (' message' , function message (msg ) {
@@ -291,9 +291,9 @@ A client WebSocket broadcasting to all connected WebSocket clients, including
291
291
itself.
292
292
293
293
``` js
294
- const WebSocket = require ( ' ws' ) ;
294
+ import WebSocket , { WebSocketServer } from ' ws' ;
295
295
296
- const wss = new WebSocket.Server ({ port: 8080 });
296
+ const wss = new WebSocketServer ({ port: 8080 });
297
297
298
298
wss .on (' connection' , function connection (ws ) {
299
299
ws .on (' message' , function incoming (data , isBinary ) {
@@ -310,9 +310,9 @@ A client WebSocket broadcasting to every other connected WebSocket clients,
310
310
excluding itself.
311
311
312
312
``` js
313
- const WebSocket = require ( ' ws' ) ;
313
+ import WebSocket , { WebSocketServer } from ' ws' ;
314
314
315
- const wss = new WebSocket.Server ({ port: 8080 });
315
+ const wss = new WebSocketServer ({ port: 8080 });
316
316
317
317
wss .on (' connection' , function connection (ws ) {
318
318
ws .on (' message' , function incoming (data , isBinary ) {
@@ -328,7 +328,7 @@ wss.on('connection', function connection(ws) {
328
328
### echo.websocket.org demo
329
329
330
330
``` js
331
- const WebSocket = require ( ' ws' ) ;
331
+ import WebSocket from ' ws' ;
332
332
333
333
const ws = new WebSocket (' wss://echo.websocket.org/' , {
334
334
origin: ' https://websocket.org'
@@ -355,13 +355,13 @@ ws.on('message', function incoming(data) {
355
355
### Use the Node.js streams API
356
356
357
357
``` js
358
- const WebSocket = require ( ' ws' ) ;
358
+ import WebSocket , { createWebSocketStream } from ' ws' ;
359
359
360
360
const ws = new WebSocket (' wss://echo.websocket.org/' , {
361
361
origin: ' https://websocket.org'
362
362
});
363
363
364
- const duplex = WebSocket . createWebSocketStream (ws, { encoding: ' utf8' });
364
+ const duplex = createWebSocketStream (ws, { encoding: ' utf8' });
365
365
366
366
duplex .pipe (process .stdout );
367
367
process .stdin .pipe (duplex);
@@ -381,9 +381,9 @@ Otherwise, see the test cases.
381
381
The remote IP address can be obtained from the raw socket.
382
382
383
383
``` js
384
- const WebSocket = require ( ' ws' ) ;
384
+ import { WebSocketServer } from ' ws' ;
385
385
386
- const wss = new WebSocket.Server ({ port: 8080 });
386
+ const wss = new WebSocketServer ({ port: 8080 });
387
387
388
388
wss .on (' connection' , function connection (ws , req ) {
389
389
const ip = req .socket .remoteAddress ;
@@ -409,15 +409,15 @@ In these cases ping messages can be used as a means to verify that the remote
409
409
endpoint is still responsive.
410
410
411
411
``` js
412
- const WebSocket = require ( ' ws' ) ;
412
+ import { WebSocketServer } from ' ws' ;
413
413
414
414
function noop () {}
415
415
416
416
function heartbeat () {
417
417
this .isAlive = true ;
418
418
}
419
419
420
- const wss = new WebSocket.Server ({ port: 8080 });
420
+ const wss = new WebSocketServer ({ port: 8080 });
421
421
422
422
wss .on (' connection' , function connection (ws ) {
423
423
ws .isAlive = true ;
@@ -446,7 +446,7 @@ without knowing it. You might want to add a ping listener on your clients to
446
446
prevent that. A simple implementation would be:
447
447
448
448
``` js
449
- const WebSocket = require ( ' ws' ) ;
449
+ import WebSocket from ' ws' ;
450
450
451
451
function heartbeat () {
452
452
clearTimeout (this .pingTimeout );
0 commit comments