Skip to content

Commit 112cb81

Browse files
committed
Bump npm ws dependency used in test driver from 0.4.31 to 7.0.0. Closes dhbaird#83.
1 parent 9bc8efb commit 112cb81

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
"url": "https://github.com/dhbaird/easywsclient/issues"
1717
},
1818
"dependencies": {
19-
"ws": "~0.4.31"
19+
"ws": "^7.0.0"
2020
}
2121
}

test/testServer.js

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
/*
2-
Prerequisites:
3-
4-
1. Install node.js and npm
5-
2. npm install ws
6-
7-
See also,
8-
9-
http://einaros.github.com/ws/
10-
11-
To run,
12-
13-
node example-server.js
14-
*/
1+
// To run,
2+
//
3+
// npm install # to install the 'ws' dependency
4+
// node testServer.js
155

166
"use strict"; // http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
177
var WebSocketServer = require('ws').Server;
188
var http = require('http');
9+
var url = require('url');
1910
var app = http.createServer();
20-
var server; // assigned by app.listen() below
11+
var server;
2112

22-
var wssEchoWithSize = new WebSocketServer({server: app, path: '/echoWithSize'});
13+
var wssEchoWithSize = new WebSocketServer({ noServer: true });
2314
wssEchoWithSize.on('connection', function(ws) {
24-
ws.on('message', function(data, flags) {
25-
if (flags.binary) { return; }
15+
ws.on('message', function(data) {
16+
if (data instanceof Buffer) { return; }
2617
ws.send(data.length + '\n' + data);
2718
});
2819
ws.on('close', function() {
@@ -31,12 +22,10 @@ wssEchoWithSize.on('connection', function(ws) {
3122
});
3223
});
3324

34-
var wssBinaryEchoWithSize = new WebSocketServer({server: app, path: '/binaryEchoWithSize'});
25+
var wssBinaryEchoWithSize = new WebSocketServer({ noServer: true });
3526
wssBinaryEchoWithSize.on('connection', function(ws) {
36-
ws.on('message', function(data, flags) {
37-
if (!flags.binary) { return; }
38-
//var result = new ArrayBuffer(data.length + 4);
39-
//new DataView(result).setInt32(0, data.length, false); // false = big endian
27+
ws.on('message', function(data) {
28+
if (!data instanceof Buffer) { return; }
4029
var result = new Buffer(data.length + 4);
4130
result.writeInt32BE(data.length, 0);
4231
data.copy(result, 4, 0, data.length);
@@ -48,10 +37,10 @@ wssBinaryEchoWithSize.on('connection', function(ws) {
4837
});
4938
});
5039

51-
var wssKillServer = new WebSocketServer({server: app, path: '/killServer'});
40+
var wssKillServer = new WebSocketServer({ noServer: true });
5241
wssKillServer.on('connection', function(ws) {
53-
ws.on('message', function(data, flags) {
54-
if (flags.binary) { return; }
42+
ws.on('message', function(data) {
43+
if (data instanceof Buffer) { return; }
5544
server.close();
5645
});
5746
ws.on('close', function() {
@@ -60,6 +49,32 @@ wssKillServer.on('connection', function(ws) {
6049
});
6150
});
6251

52+
app.on('upgrade', function(request, socket, head) {
53+
var pathname = url.parse(request.url).pathname;
54+
55+
if ('/echoWithSize' === pathname) {
56+
wssEchoWithSize.handleUpgrade(request, socket, head, function(ws) {
57+
wssEchoWithSize.emit('connection', ws, request);
58+
});
59+
}
60+
61+
else if ('/binaryEchoWithSize' === pathname) {
62+
wssBinaryEchoWithSize.handleUpgrade(request, socket, head, function(ws) {
63+
wssBinaryEchoWithSize.emit('connection', ws, request);
64+
});
65+
}
66+
67+
else if ('/killServer' === pathname) {
68+
wssKillServer.handleUpgrade(request, socket, head, function(ws) {
69+
wssKillServer.emit('connection', ws, request);
70+
});
71+
}
72+
73+
else {
74+
socket.destroy();
75+
}
76+
});
77+
6378
var PORT = parseInt(process.env.PORT || '8123');
6479
server = app.listen(PORT);
6580
console.log('Listening on port ' + PORT + '...');

0 commit comments

Comments
 (0)