Skip to content

Commit

Permalink
[doc] Add error handlers to examples and code snippets
Browse files Browse the repository at this point in the history
Closes #2112
  • Loading branch information
lpinca committed Jan 23, 2023
1 parent a3214d3 commit 2862c2f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ import WebSocket from 'ws';

const ws = new WebSocket('ws://www.host.com/path');

ws.on('error', console.error);

ws.on('open', function open() {
ws.send('something');
});
Expand All @@ -171,6 +173,8 @@ import WebSocket from 'ws';

const ws = new WebSocket('ws://www.host.com/path');

ws.on('error', console.error);

ws.on('open', function open() {
const array = new Float32Array(5);

Expand All @@ -190,6 +194,8 @@ import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.on('error', console.error);

ws.on('message', function message(data) {
console.log('received: %s', data);
});
Expand All @@ -212,6 +218,8 @@ const server = createServer({
const wss = new WebSocketServer({ server });

wss.on('connection', function connection(ws) {
ws.on('error', console.error);

ws.on('message', function message(data) {
console.log('received: %s', data);
});
Expand All @@ -234,10 +242,14 @@ const wss1 = new WebSocketServer({ noServer: true });
const wss2 = new WebSocketServer({ noServer: true });

wss1.on('connection', function connection(ws) {
ws.on('error', console.error);

// ...
});

wss2.on('connection', function connection(ws) {
ws.on('error', console.error);

// ...
});

Expand Down Expand Up @@ -266,16 +278,24 @@ server.listen(8080);
import { createServer } from 'http';
import { WebSocketServer } from 'ws';

function onSocketError(err) {
console.error(err);
}

const server = createServer();
const wss = new WebSocketServer({ noServer: true });

wss.on('connection', function connection(ws, request, client) {
ws.on('error', console.error);

ws.on('message', function message(data) {
console.log(`Received message ${data} from user ${client}`);
});
});

server.on('upgrade', function upgrade(request, socket, head) {
socket.on('error', onSocketError);

// This function is not defined on purpose. Implement it with your own logic.
authenticate(request, function next(err, client) {
if (err || !client) {
Expand All @@ -284,6 +304,8 @@ server.on('upgrade', function upgrade(request, socket, head) {
return;
}

socket.removeListener('error', onSocketError);

wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit('connection', ws, request, client);
});
Expand All @@ -306,6 +328,8 @@ import WebSocket, { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.on('error', console.error);

ws.on('message', function message(data, isBinary) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
Expand All @@ -325,6 +349,8 @@ import WebSocket, { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.on('error', console.error);

ws.on('message', function message(data, isBinary) {
wss.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
Expand All @@ -342,6 +368,8 @@ import WebSocket from 'ws';

const ws = new WebSocket('wss://websocket-echo.com/');

ws.on('error', console.error);

ws.on('open', function open() {
console.log('connected');
ws.send(Date.now());
Expand Down Expand Up @@ -369,6 +397,8 @@ const ws = new WebSocket('wss://websocket-echo.com/');

const duplex = createWebSocketStream(ws, { encoding: 'utf8' });

duplex.on('error', console.error);

duplex.pipe(process.stdout);
process.stdin.pipe(duplex);
```
Expand All @@ -393,6 +423,8 @@ const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws, req) {
const ip = req.socket.remoteAddress;

ws.on('error', console.error);
});
```

Expand All @@ -402,6 +434,8 @@ the `X-Forwarded-For` header.
```js
wss.on('connection', function connection(ws, req) {
const ip = req.headers['x-forwarded-for'].split(',')[0].trim();

ws.on('error', console.error);
});
```

Expand All @@ -425,6 +459,7 @@ const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.isAlive = true;
ws.on('error', console.error);
ws.on('pong', heartbeat);
});

Expand Down Expand Up @@ -466,6 +501,7 @@ function heartbeat() {

const client = new WebSocket('wss://websocket-echo.com/');

client.on('error', console.error);
client.on('open', heartbeat);
client.on('ping', heartbeat);
client.on('close', function clear() {
Expand Down
10 changes: 10 additions & 0 deletions examples/express-session-parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const uuid = require('uuid');

const { WebSocketServer } = require('../..');

function onSocketError(err) {
console.error(err);
}

const app = express();
const map = new Map();

Expand Down Expand Up @@ -59,6 +63,8 @@ const server = http.createServer(app);
const wss = new WebSocketServer({ clientTracking: false, noServer: true });

server.on('upgrade', function (request, socket, head) {
socket.on('error', onSocketError);

console.log('Parsing session from request...');

sessionParser(request, {}, () => {
Expand All @@ -70,6 +76,8 @@ server.on('upgrade', function (request, socket, head) {

console.log('Session is parsed!');

socket.removeListener('error', onSocketError);

wss.handleUpgrade(request, socket, head, function (ws) {
wss.emit('connection', ws, request);
});
Expand All @@ -81,6 +89,8 @@ wss.on('connection', function (ws, request) {

map.set(userId, ws);

ws.on('error', console.error);

ws.on('message', function (message) {
//
// Here we can now use session parameters.
Expand Down
2 changes: 2 additions & 0 deletions examples/server-stats/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ wss.on('connection', function (ws) {
}, 100);
console.log('started client interval');

ws.on('error', console.error);

ws.on('close', function () {
console.log('stopping client interval');
clearInterval(id);
Expand Down
4 changes: 4 additions & 0 deletions examples/ssl.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const server = https.createServer({
const wss = new WebSocketServer({ server });

wss.on('connection', function connection(ws) {
ws.on('error', console.error);

ws.on('message', function message(msg) {
console.log(msg.toString());
});
Expand All @@ -31,6 +33,8 @@ server.listen(function listening() {
rejectUnauthorized: false
});

ws.on('error', console.error);

ws.on('open', function open() {
ws.send('All glory to WebSockets!');
});
Expand Down

0 comments on commit 2862c2f

Please sign in to comment.