@@ -155,6 +155,8 @@ import WebSocket from 'ws';
155
155
156
156
const ws = new WebSocket (' ws://www.host.com/path' );
157
157
158
+ ws .on (' error' , console .error );
159
+
158
160
ws .on (' open' , function open () {
159
161
ws .send (' something' );
160
162
});
@@ -171,6 +173,8 @@ import WebSocket from 'ws';
171
173
172
174
const ws = new WebSocket (' ws://www.host.com/path' );
173
175
176
+ ws .on (' error' , console .error );
177
+
174
178
ws .on (' open' , function open () {
175
179
const array = new Float32Array (5 );
176
180
@@ -190,6 +194,8 @@ import { WebSocketServer } from 'ws';
190
194
const wss = new WebSocketServer ({ port: 8080 });
191
195
192
196
wss .on (' connection' , function connection (ws ) {
197
+ ws .on (' error' , console .error );
198
+
193
199
ws .on (' message' , function message (data ) {
194
200
console .log (' received: %s' , data);
195
201
});
@@ -212,6 +218,8 @@ const server = createServer({
212
218
const wss = new WebSocketServer ({ server });
213
219
214
220
wss .on (' connection' , function connection (ws ) {
221
+ ws .on (' error' , console .error );
222
+
215
223
ws .on (' message' , function message (data ) {
216
224
console .log (' received: %s' , data);
217
225
});
@@ -234,10 +242,14 @@ const wss1 = new WebSocketServer({ noServer: true });
234
242
const wss2 = new WebSocketServer ({ noServer: true });
235
243
236
244
wss1 .on (' connection' , function connection (ws ) {
245
+ ws .on (' error' , console .error );
246
+
237
247
// ...
238
248
});
239
249
240
250
wss2 .on (' connection' , function connection (ws ) {
251
+ ws .on (' error' , console .error );
252
+
241
253
// ...
242
254
});
243
255
@@ -266,16 +278,24 @@ server.listen(8080);
266
278
import { createServer } from ' http' ;
267
279
import { WebSocketServer } from ' ws' ;
268
280
281
+ function onSocketError (err ) {
282
+ console .error (err);
283
+ }
284
+
269
285
const server = createServer ();
270
286
const wss = new WebSocketServer ({ noServer: true });
271
287
272
288
wss .on (' connection' , function connection (ws , request , client ) {
289
+ ws .on (' error' , console .error );
290
+
273
291
ws .on (' message' , function message (data ) {
274
292
console .log (` Received message ${ data} from user ${ client} ` );
275
293
});
276
294
});
277
295
278
296
server .on (' upgrade' , function upgrade (request , socket , head ) {
297
+ socket .on (' error' , onSocketError);
298
+
279
299
// This function is not defined on purpose. Implement it with your own logic.
280
300
authenticate (request, function next (err , client ) {
281
301
if (err || ! client) {
@@ -284,6 +304,8 @@ server.on('upgrade', function upgrade(request, socket, head) {
284
304
return ;
285
305
}
286
306
307
+ socket .removeListener (' error' , onSocketError);
308
+
287
309
wss .handleUpgrade (request, socket, head, function done (ws ) {
288
310
wss .emit (' connection' , ws, request, client);
289
311
});
@@ -306,6 +328,8 @@ import WebSocket, { WebSocketServer } from 'ws';
306
328
const wss = new WebSocketServer ({ port: 8080 });
307
329
308
330
wss .on (' connection' , function connection (ws ) {
331
+ ws .on (' error' , console .error );
332
+
309
333
ws .on (' message' , function message (data , isBinary ) {
310
334
wss .clients .forEach (function each (client ) {
311
335
if (client .readyState === WebSocket .OPEN ) {
@@ -325,6 +349,8 @@ import WebSocket, { WebSocketServer } from 'ws';
325
349
const wss = new WebSocketServer ({ port: 8080 });
326
350
327
351
wss .on (' connection' , function connection (ws ) {
352
+ ws .on (' error' , console .error );
353
+
328
354
ws .on (' message' , function message (data , isBinary ) {
329
355
wss .clients .forEach (function each (client ) {
330
356
if (client !== ws && client .readyState === WebSocket .OPEN ) {
@@ -342,6 +368,8 @@ import WebSocket from 'ws';
342
368
343
369
const ws = new WebSocket (' wss://websocket-echo.com/' );
344
370
371
+ ws .on (' error' , console .error );
372
+
345
373
ws .on (' open' , function open () {
346
374
console .log (' connected' );
347
375
ws .send (Date .now ());
@@ -369,6 +397,8 @@ const ws = new WebSocket('wss://websocket-echo.com/');
369
397
370
398
const duplex = createWebSocketStream (ws, { encoding: ' utf8' });
371
399
400
+ duplex .on (' error' , console .error );
401
+
372
402
duplex .pipe (process .stdout );
373
403
process .stdin .pipe (duplex);
374
404
```
@@ -393,6 +423,8 @@ const wss = new WebSocketServer({ port: 8080 });
393
423
394
424
wss .on (' connection' , function connection (ws , req ) {
395
425
const ip = req .socket .remoteAddress ;
426
+
427
+ ws .on (' error' , console .error );
396
428
});
397
429
```
398
430
@@ -402,6 +434,8 @@ the `X-Forwarded-For` header.
402
434
``` js
403
435
wss .on (' connection' , function connection (ws , req ) {
404
436
const ip = req .headers [' x-forwarded-for' ].split (' ,' )[0 ].trim ();
437
+
438
+ ws .on (' error' , console .error );
405
439
});
406
440
```
407
441
@@ -425,6 +459,7 @@ const wss = new WebSocketServer({ port: 8080 });
425
459
426
460
wss .on (' connection' , function connection (ws ) {
427
461
ws .isAlive = true ;
462
+ ws .on (' error' , console .error );
428
463
ws .on (' pong' , heartbeat);
429
464
});
430
465
@@ -466,6 +501,7 @@ function heartbeat() {
466
501
467
502
const client = new WebSocket (' wss://websocket-echo.com/' );
468
503
504
+ client .on (' error' , console .error );
469
505
client .on (' open' , heartbeat);
470
506
client .on (' ping' , heartbeat);
471
507
client .on (' close' , function clear () {
0 commit comments