Skip to content

Commit 7bec220

Browse files
committed
[doc] Modernize examples
1 parent fd910f1 commit 7bec220

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

README.md

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ compiler is installed on the host system.
4343

4444
```js
4545
const WebSocket = require('ws');
46+
4647
const ws = new WebSocket('ws://www.host.com/path');
4748

4849
ws.on('open', function open() {
4950
ws.send('something');
5051
});
5152

52-
ws.on('message', function(data, flags) {
53+
ws.on('message', function incoming(data, flags) {
5354
// flags.binary will be set if a binary data is received.
5455
// flags.masked will be set if the data was masked.
5556
});
@@ -59,28 +60,26 @@ ws.on('message', function(data, flags) {
5960

6061
```js
6162
const WebSocket = require('ws');
63+
6264
const ws = new WebSocket('ws://www.host.com/path');
6365

6466
ws.on('open', function open() {
65-
var array = new Float32Array(5);
67+
const array = new Float32Array(5);
6668

6769
for (var i = 0; i < array.length; ++i) {
6870
array[i] = i / 2;
6971
}
7072

71-
ws.send(array, { binary: true, mask: true });
73+
ws.send(array);
7274
});
7375
```
7476

75-
Setting `mask`, as done for the send options above, will cause the data to be
76-
masked according to the WebSocket protocol. The same option applies for text
77-
data.
78-
7977
### Server example
8078

8179
```js
82-
const WebSocketServer = require('ws').Server;
83-
const wss = new WebSocketServer({ port: 8080 });
80+
const WebSocket = require('ws');
81+
82+
const wss = new WebSocket.Server({ port: 8080 });
8483

8584
wss.on('connection', function connection(ws) {
8685
ws.on('message', function incoming(message) {
@@ -94,21 +93,23 @@ wss.on('connection', function connection(ws) {
9493
### ExpressJS example
9594

9695
```js
97-
const server = require('http').createServer();
98-
const url = require('url');
99-
const WebSocketServer = require('ws').Server;
100-
const wss = new WebSocketServer({ server: server });
10196
const express = require('express');
97+
const http = require('http');
98+
const url = require('url');
99+
const WebSocket = require('ws');
100+
102101
const app = express();
103-
const port = 4080;
104102

105103
app.use(function (req, res) {
106104
res.send({ msg: "hello" });
107105
});
108106

107+
const server = http.createServer(app);
108+
const wss = new WebSocket.Server({ server });
109+
109110
wss.on('connection', function connection(ws) {
110-
var location = url.parse(ws.upgradeReq.url, true);
111-
// you might use location.query.access_token to authenticate or share sessions
111+
const location = url.parse(ws.upgradeReq.url, true);
112+
// You might use location.query.access_token to authenticate or share sessions
112113
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
113114

114115
ws.on('message', function incoming(message) {
@@ -118,28 +119,34 @@ wss.on('connection', function connection(ws) {
118119
ws.send('something');
119120
});
120121

121-
server.on('request', app);
122-
server.listen(port, function () { console.log('Listening on ' + server.address().port) });
122+
server.listen(8080, function listening() {
123+
console.log('Listening on %d', server.address().port);
124+
});
123125
```
124126

125127
### Server sending broadcast data
126128

127129
```js
128-
const WebSocketServer = require('ws').Server;
129-
const wss = new WebSocketServer({ port: 8080 });
130+
const WebSocket = require('ws');
131+
132+
const wss = new WebSocket.Server({ port: 8080 });
130133

131134
// Broadcast to all.
132135
wss.broadcast = function broadcast(data) {
133136
wss.clients.forEach(function each(client) {
134-
client.send(data);
137+
if (client.readyState === WebSocket.OPEN) {
138+
client.send(data);
139+
}
135140
});
136141
};
137142

138143
wss.on('connection', function connection(ws) {
139-
ws.on('message', function message(data) {
144+
ws.on('message', function incoming(data) {
140145
// Broadcast to everyone else.
141146
wss.clients.forEach(function each(client) {
142-
if (client !== ws) client.send(data);
147+
if (client !== ws && client.readyState === WebSocket.OPEN) {
148+
client.send(data);
149+
}
143150
});
144151
});
145152
});
@@ -155,11 +162,11 @@ ws.send('something');
155162
// callback. The callback is also the only way of being notified that data has
156163
// actually been sent.
157164
ws.send('something', function ack(error) {
158-
// if error is not defined, the send has been completed,
159-
// otherwise the error object will indicate what failed.
165+
// If error is not defined, the send has been completed, otherwise the error
166+
// object will indicate what failed.
160167
});
161168

162-
// Immediate errors can also be handled with try/catch-blocks, but **note** that
169+
// Immediate errors can also be handled with `try...catch`, but **note** that
163170
// since sends are inherently asynchronous, socket write failures will *not* be
164171
// captured when this technique is used.
165172
try { ws.send('something'); }
@@ -170,25 +177,25 @@ catch (e) { /* handle error */ }
170177

171178
```js
172179
const WebSocket = require('ws');
173-
const ws = new WebSocket('ws://echo.websocket.org/', {
174-
protocolVersion: 8,
175-
origin: 'http://websocket.org'
180+
181+
const ws = new WebSocket('wss://echo.websocket.org/', {
182+
origin: 'https://websocket.org'
176183
});
177184

178185
ws.on('open', function open() {
179186
console.log('connected');
180-
ws.send(Date.now().toString(), {mask: true});
187+
ws.send(Date.now());
181188
});
182189

183190
ws.on('close', function close() {
184191
console.log('disconnected');
185192
});
186193

187-
ws.on('message', function message(data, flags) {
188-
console.log('Roundtrip time: ' + (Date.now() - parseInt(data)) + 'ms', flags);
194+
ws.on('message', function incoming(data, flags) {
195+
console.log(`Roundtrip time: ${Date.now() - data} ms`, flags);
189196

190197
setTimeout(function timeout() {
191-
ws.send(Date.now().toString(), {mask: true});
198+
ws.send(Date.now());
192199
}, 500);
193200
});
194201
```
@@ -198,18 +205,17 @@ ws.on('message', function message(data, flags) {
198205
For a full example with a browser client communicating with a ws server, see the
199206
examples folder.
200207

201-
Note that the usage together with Express 3.0 is quite different from Express
202-
2.x. The difference is expressed in the two different serverstats-examples.
203-
204208
Otherwise, see the test cases.
205209

206210
## API Docs
207211

208-
See [`/doc/ws.md`](https://github.com/websockets/ws/blob/master/doc/ws.md) for Node.js-like docs for the ws classes.
212+
See [`/doc/ws.md`](https://github.com/websockets/ws/blob/master/doc/ws.md)
213+
for Node.js-like docs for the ws classes.
209214

210215
## Changelog
211216

212-
We're using the GitHub [`releases`](https://github.com/websockets/ws/releases) for changelog entries.
217+
We're using the GitHub [`releases`](https://github.com/websockets/ws/releases)
218+
for changelog entries.
213219

214220
## License
215221

0 commit comments

Comments
 (0)