@@ -43,13 +43,14 @@ compiler is installed on the host system.
43
43
44
44
``` js
45
45
const WebSocket = require (' ws' );
46
+
46
47
const ws = new WebSocket (' ws://www.host.com/path' );
47
48
48
49
ws .on (' open' , function open () {
49
50
ws .send (' something' );
50
51
});
51
52
52
- ws .on (' message' , function (data , flags ) {
53
+ ws .on (' message' , function incoming (data , flags ) {
53
54
// flags.binary will be set if a binary data is received.
54
55
// flags.masked will be set if the data was masked.
55
56
});
@@ -59,28 +60,26 @@ ws.on('message', function(data, flags) {
59
60
60
61
``` js
61
62
const WebSocket = require (' ws' );
63
+
62
64
const ws = new WebSocket (' ws://www.host.com/path' );
63
65
64
66
ws .on (' open' , function open () {
65
- var array = new Float32Array (5 );
67
+ const array = new Float32Array (5 );
66
68
67
69
for (var i = 0 ; i < array .length ; ++ i) {
68
70
array[i] = i / 2 ;
69
71
}
70
72
71
- ws .send (array, { binary : true , mask : true } );
73
+ ws .send (array);
72
74
});
73
75
```
74
76
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
-
79
77
### Server example
80
78
81
79
``` 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 });
84
83
85
84
wss .on (' connection' , function connection (ws ) {
86
85
ws .on (' message' , function incoming (message ) {
@@ -94,21 +93,23 @@ wss.on('connection', function connection(ws) {
94
93
### ExpressJS example
95
94
96
95
``` 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 });
101
96
const express = require (' express' );
97
+ const http = require (' http' );
98
+ const url = require (' url' );
99
+ const WebSocket = require (' ws' );
100
+
102
101
const app = express ();
103
- const port = 4080 ;
104
102
105
103
app .use (function (req , res ) {
106
104
res .send ({ msg: " hello" });
107
105
});
108
106
107
+ const server = http .createServer (app);
108
+ const wss = new WebSocket.Server ({ server });
109
+
109
110
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
112
113
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
113
114
114
115
ws .on (' message' , function incoming (message ) {
@@ -118,28 +119,34 @@ wss.on('connection', function connection(ws) {
118
119
ws .send (' something' );
119
120
});
120
121
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
+ });
123
125
```
124
126
125
127
### Server sending broadcast data
126
128
127
129
``` 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 });
130
133
131
134
// Broadcast to all.
132
135
wss .broadcast = function broadcast (data ) {
133
136
wss .clients .forEach (function each (client ) {
134
- client .send (data);
137
+ if (client .readyState === WebSocket .OPEN ) {
138
+ client .send (data);
139
+ }
135
140
});
136
141
};
137
142
138
143
wss .on (' connection' , function connection (ws ) {
139
- ws .on (' message' , function message (data ) {
144
+ ws .on (' message' , function incoming (data ) {
140
145
// Broadcast to everyone else.
141
146
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
+ }
143
150
});
144
151
});
145
152
});
@@ -155,11 +162,11 @@ ws.send('something');
155
162
// callback. The callback is also the only way of being notified that data has
156
163
// actually been sent.
157
164
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.
160
167
});
161
168
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
163
170
// since sends are inherently asynchronous, socket write failures will *not* be
164
171
// captured when this technique is used.
165
172
try { ws .send (' something' ); }
@@ -170,25 +177,25 @@ catch (e) { /* handle error */ }
170
177
171
178
``` js
172
179
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'
176
183
});
177
184
178
185
ws .on (' open' , function open () {
179
186
console .log (' connected' );
180
- ws .send (Date .now (). toString (), {mask : true } );
187
+ ws .send (Date .now ());
181
188
});
182
189
183
190
ws .on (' close' , function close () {
184
191
console .log (' disconnected' );
185
192
});
186
193
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);
189
196
190
197
setTimeout (function timeout () {
191
- ws .send (Date .now (). toString (), {mask : true } );
198
+ ws .send (Date .now ());
192
199
}, 500 );
193
200
});
194
201
```
@@ -198,18 +205,17 @@ ws.on('message', function message(data, flags) {
198
205
For a full example with a browser client communicating with a ws server, see the
199
206
examples folder.
200
207
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
-
204
208
Otherwise, see the test cases.
205
209
206
210
## API Docs
207
211
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.
209
214
210
215
## Changelog
211
216
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.
213
219
214
220
## License
215
221
0 commit comments