Bump npm ws dependency used in test driver from 0.4.31 to 7.0.0. Closes #83.

This commit is contained in:
David Baird
2019-05-19 09:03:37 -04:00
parent 9bc8efb765
commit 112cb81567
2 changed files with 42 additions and 27 deletions
+1 -1
View File
@@ -16,6 +16,6 @@
"url": "https://github.com/dhbaird/easywsclient/issues"
},
"dependencies": {
"ws": "~0.4.31"
"ws": "^7.0.0"
}
}
+41 -26
View File
@@ -1,28 +1,19 @@
/*
Prerequisites:
1. Install node.js and npm
2. npm install ws
See also,
http://einaros.github.com/ws/
To run,
node example-server.js
*/
// To run,
//
// npm install # to install the 'ws' dependency
// node testServer.js
"use strict"; // http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
var WebSocketServer = require('ws').Server;
var http = require('http');
var url = require('url');
var app = http.createServer();
var server; // assigned by app.listen() below
var server;
var wssEchoWithSize = new WebSocketServer({server: app, path: '/echoWithSize'});
var wssEchoWithSize = new WebSocketServer({ noServer: true });
wssEchoWithSize.on('connection', function(ws) {
ws.on('message', function(data, flags) {
if (flags.binary) { return; }
ws.on('message', function(data) {
if (data instanceof Buffer) { return; }
ws.send(data.length + '\n' + data);
});
ws.on('close', function() {
@@ -31,12 +22,10 @@ wssEchoWithSize.on('connection', function(ws) {
});
});
var wssBinaryEchoWithSize = new WebSocketServer({server: app, path: '/binaryEchoWithSize'});
var wssBinaryEchoWithSize = new WebSocketServer({ noServer: true });
wssBinaryEchoWithSize.on('connection', function(ws) {
ws.on('message', function(data, flags) {
if (!flags.binary) { return; }
//var result = new ArrayBuffer(data.length + 4);
//new DataView(result).setInt32(0, data.length, false); // false = big endian
ws.on('message', function(data) {
if (!data instanceof Buffer) { return; }
var result = new Buffer(data.length + 4);
result.writeInt32BE(data.length, 0);
data.copy(result, 4, 0, data.length);
@@ -48,10 +37,10 @@ wssBinaryEchoWithSize.on('connection', function(ws) {
});
});
var wssKillServer = new WebSocketServer({server: app, path: '/killServer'});
var wssKillServer = new WebSocketServer({ noServer: true });
wssKillServer.on('connection', function(ws) {
ws.on('message', function(data, flags) {
if (flags.binary) { return; }
ws.on('message', function(data) {
if (data instanceof Buffer) { return; }
server.close();
});
ws.on('close', function() {
@@ -60,6 +49,32 @@ wssKillServer.on('connection', function(ws) {
});
});
app.on('upgrade', function(request, socket, head) {
var pathname = url.parse(request.url).pathname;
if ('/echoWithSize' === pathname) {
wssEchoWithSize.handleUpgrade(request, socket, head, function(ws) {
wssEchoWithSize.emit('connection', ws, request);
});
}
else if ('/binaryEchoWithSize' === pathname) {
wssBinaryEchoWithSize.handleUpgrade(request, socket, head, function(ws) {
wssBinaryEchoWithSize.emit('connection', ws, request);
});
}
else if ('/killServer' === pathname) {
wssKillServer.handleUpgrade(request, socket, head, function(ws) {
wssKillServer.emit('connection', ws, request);
});
}
else {
socket.destroy();
}
});
var PORT = parseInt(process.env.PORT || '8123');
server = app.listen(PORT);
console.log('Listening on port ' + PORT + '...');