From 7a7c12825033c4f72400600709f34fc22d686617 Mon Sep 17 00:00:00 2001 From: Ash Wilson Date: Tue, 31 Mar 2015 13:28:58 +0100 Subject: [PATCH] Add support for SockJS --- README.md | 1 + lib/worker.js | 3 ++ lib/workers/sockjsworker.js | 65 +++++++++++++++++++++++++++++++++++++ package.json | 3 +- 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 lib/workers/sockjsworker.js diff --git a/README.md b/README.md index 59f95dd..cc42eca 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Nodejs cli tool for benchmarking websocket servers. Currently supports: * [Engine.IO](https://github.com/LearnBoost/engine.io) * [Faye](https://github.com/faye/faye) * [Primus](https://github.com/primus/primus) +* [SockJS](https://github.com/sockjs/sockjs-client) ## Installation diff --git a/lib/worker.js b/lib/worker.js index c5b3e00..a7f2284 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -26,6 +26,9 @@ switch (workerType) { case 'primus': BenchmarkWorker = require('./workers/primusworker.js'); break; + case 'sockjs': + BenchmarkWorker = require('./workers/sockjsworker.js'); + break; default: logger.error('error workerType ' + workerType); } diff --git a/lib/workers/sockjsworker.js b/lib/workers/sockjsworker.js new file mode 100644 index 0000000..e57cb71 --- /dev/null +++ b/lib/workers/sockjsworker.js @@ -0,0 +1,65 @@ +/*global module, require*/ +var util = require('util'), + SockJS = require('sockjs-client'), + BaseWorker = require('./baseworker.js'), + logger = require('../logger.js'); + +var pid = process.pid; + +/** + * SockJSWorker Worker class inherits form BaseWorker + */ +var SockJSWorker = function (server, generator) { + SockJSWorker.super_.apply(this, arguments); +}; + +util.inherits(SockJSWorker, BaseWorker); + +SockJSWorker.prototype.createClient = function (callback) { + var self = this; + + var connect = function (session) { + var client = new SockJS(self.server, undefined, { + timeout: 90 * 1000, + devel: false, + debug: false, + protocols_whitelist: ['websocket'], + info: { "websocket": true, "origins": ["*:*"], "cookie_needed": false, "entropy": 1739246751 } + }); + client.disconnect = function () { + client.close(); + }; + client.session = session; + + client.onopen = function () { + client.open = true; + if (self.verbose) { + logger.error("[" + pid + "][WS][SockJS][OPEN]"); + } + callback(false, client); + }; + + client.onerror = function () { + if (self.verbose) { + logger.error("[" + pid + "][WS][SockJS][ERROR]"); + } + callback(true, client); + }; + + client.onclose = function (closeEvent) { + if (!client.open && closeEvent.code !== 1000) { + callback(true, client); + logger.error("*** Client close: " + util.inspect(closeEvent) + "Error:" + new Error().stack); + } + client.session = null; + if (self.verbose) { + logger.error("[" + pid + "][WS][SockJS][CLOSE]"); + } + }; + }; + + connect({}); + +}; + +module.exports = SockJSWorker; \ No newline at end of file diff --git a/package.json b/package.json index 5cce4d1..ffa754e 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "faye": "~0.8.9", "winston": "~0.7.1", "socket.io": "~0.9.14", - "primus": "~1.5.2" + "primus": "~1.5.2", + "sockjs-client": "^1.0.0-beta.12" }, "devDependencies": { "chai": "~1.6.0",