From 112cb8156712f941e37616d5ed9a9e48e2e6e48a Mon Sep 17 00:00:00 2001 From: David Baird Date: Sun, 19 May 2019 09:03:37 -0400 Subject: [PATCH] Bump npm ws dependency used in test driver from 0.4.31 to 7.0.0. Closes #83. --- package.json | 2 +- test/testServer.js | 67 ++++++++++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index df45636..b416629 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,6 @@ "url": "https://github.com/dhbaird/easywsclient/issues" }, "dependencies": { - "ws": "~0.4.31" + "ws": "^7.0.0" } } diff --git a/test/testServer.js b/test/testServer.js index 2d456a1..bece2b4 100644 --- a/test/testServer.js +++ b/test/testServer.js @@ -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 + '...');