From 98164773b29d43a8cb16a0359c81d2a3b717195b Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 5 Nov 2019 13:24:26 +0100 Subject: [PATCH] Fixes #9 - Add support for WebSocket. Implemented WebSocket support via the "ws" package. Signed-off-by: Simone Bordet --- cometd-nodejs-client.js | 3 +++ package.json | 5 ++-- test/client.js | 1 + test/proxy.js | 8 +++++++ test/websocket.js | 53 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 test/websocket.js diff --git a/cometd-nodejs-client.js b/cometd-nodejs-client.js index 0cc153e..997f741 100644 --- a/cometd-nodejs-client.js +++ b/cometd-nodejs-client.js @@ -5,6 +5,7 @@ module.exports = { var https = require('https'); var HttpcProxyAgent = require('http-proxy-agent'); var HttpsProxyAgent = require('https-proxy-agent'); + var ws = require('ws'); var window = global['window']; if (!window) { @@ -221,5 +222,7 @@ module.exports = { window.XMLHttpRequest.HEADERS_RECEIVED = 2; window.XMLHttpRequest.LOADING = 3; window.XMLHttpRequest.DONE = 4; + + window.WebSocket = ws; } }; diff --git a/package.json b/package.json index fd1dbb1..ab53abc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cometd-nodejs-client", - "version": "1.1.0", + "version": "2.0.0-BETA0", "description": "Adapter code to run the CometD JavaScript library in a NodeJS environment", "keywords": [ "node", @@ -23,7 +23,8 @@ "dependencies": { "cometd": ">=3.1.2", "http-proxy-agent": "^2.1.0", - "https-proxy-agent": "^2.1.0" + "https-proxy-agent": "^2.1.0", + "ws": "^7.2.0" }, "devDependencies": { "mocha": "^6.2.2" diff --git a/test/client.js b/test/client.js index 827147f..9cb2d07 100644 --- a/test/client.js +++ b/test/client.js @@ -42,6 +42,7 @@ describe('client', function() { console.log('listening on localhost:' + port); var cometd = new _lib.CometD(); + cometd.websocketEnabled = false; cometd.configure({ url: 'http://localhost:' + port + '/cometd', logLevel: 'info' diff --git a/test/proxy.js b/test/proxy.js index bab0551..019bcc9 100644 --- a/test/proxy.js +++ b/test/proxy.js @@ -52,6 +52,7 @@ describe('proxy', function() { // Any port will do for the server. var serverPort = proxyPort + 1; var cometd = new _lib.CometD(); + cometd.websocketEnabled = false; cometd.configure({ url: 'http://localhost:' + serverPort + '/cometd', logLevel: 'info' @@ -108,6 +109,7 @@ describe('proxy', function() { }); var cometd1 = new _lib.CometD(); + cometd1.websocketEnabled = false; cometd1.configure({ url: 'http://localhost:' + serverPort1 + '/cometd', logLevel: 'info' @@ -115,6 +117,7 @@ describe('proxy', function() { cometd1.handshake(function(r) { if (r.successful) { var cometd2 = new _lib.CometD(); + cometd2.websocketEnabled = false; cometd2.configure({ url: 'http://localhost:' + serverPort2 + '/cometd', logLevel: 'info' @@ -176,6 +179,7 @@ describe('proxy', function() { }); var cometd1 = new _lib.CometD(); + cometd1.websocketEnabled = false; cometd1.configure({ url: 'http://localhost:' + serverPort1 + '/cometd', logLevel: 'info' @@ -187,6 +191,7 @@ describe('proxy', function() { // Stop /meta/handshake retries. cometd1.disconnect(); var cometd2 = new _lib.CometD(); + cometd2.websocketEnabled = false; cometd2.configure({ url: 'http://localhost:' + serverPort2 + '/cometd', logLevel: 'info' @@ -246,6 +251,7 @@ describe('proxy', function() { }); var cometd1 = new _lib.CometD(); + cometd1.websocketEnabled = false; cometd1.configure({ url: 'http://localhost:' + serverPort1 + '/cometd', logLevel: 'info' @@ -257,6 +263,7 @@ describe('proxy', function() { // Stop /meta/handshake retries. cometd1.disconnect(); var cometd2 = new _lib.CometD(); + cometd2.websocketEnabled = false; cometd2.configure({ url: 'http://localhost:' + serverPort2 + '/cometd', logLevel: 'info' @@ -313,6 +320,7 @@ describe('proxy', function() { // Any port will do for the server. var serverPort = proxyPort + 1; var cometd = new _lib.CometD(); + cometd.websocketEnabled = false; cometd.configure({ url: 'http://localhost:' + serverPort + '/cometd', logLevel: 'info' diff --git a/test/websocket.js b/test/websocket.js new file mode 100644 index 0000000..ca8e207 --- /dev/null +++ b/test/websocket.js @@ -0,0 +1,53 @@ +var assert = require('assert'); +var nodeCometD = require('..'); +var ws = require("ws"); + +describe('websocket', function() { + var _lib = require('cometd'); + var _server; + + afterEach(function() { + if (_server) { + _server.close(); + } + }); + + it('handshakes with websocket', function(done) { + _server = new ws.Server({port: 0}, function() { + _server.on('connection', function(s) { + s.on('message', function(m) { + var handshake = JSON.parse(m)[0]; + assert.strictEqual('/meta/handshake', handshake.channel); + assert.ok(handshake.supportedConnectionTypes.indexOf('websocket') >= 0); + var reply = [{ + id: handshake.id, + channel: handshake.channel, + successful: true, + version: "1.0", + supportedConnectionTypes: ['websocket'], + clientId: '0123456789abcdef', + advice: { + reconnect: 'none' + } + }]; + s.send(JSON.stringify(reply)); + }); + }); + + nodeCometD.adapt(); + var cometd = new _lib.CometD(); + cometd.configure({ + url: 'http://localhost:' + _server.address().port, + logLevel: 'info' + }); + cometd.handshake(function(r) { + if (r.successful) { + done(); + } else { + cometd.disconnect(); + done(new Error('could not handshake')); + } + }); + }); + }); +});