From 2ef805ac55ecd8aa317b74241f1b60776faedf00 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 10 May 2017 10:48:24 +0200 Subject: [PATCH] Fixes #4 - Isolate cookies from multiple clients. Now an external context (scoped by the application) is queried on the XMLHttpRequest instance, and possibly used to store cookies. --- cometd-nodejs-client.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cometd-nodejs-client.js b/cometd-nodejs-client.js index f9a849a..4bfefe4 100644 --- a/cometd-nodejs-client.js +++ b/cometd-nodejs-client.js @@ -15,7 +15,7 @@ module.exports = { var _agent = new http.Agent({ keepAlive: true }); - var _cookies = []; + var _cookieStore = {}; // Bare minimum XMLHttpRequest implementation that works with CometD. window.XMLHttpRequest = function() { @@ -40,7 +40,11 @@ module.exports = { }; this.send = function(data) { - var list = _cookies[_config.hostname]; + var cookieStore = this.context && this.context.cookieStore; + if (!cookieStore) { + cookieStore = _cookieStore; + } + var list = cookieStore[_config.hostname]; if (list) { var cookies = ''; for (var i = 0; i < list.length; ++i) { @@ -71,9 +75,9 @@ module.exports = { var cookie = parts[0]; var host = _config.hostname; - var list = _cookies[host]; + var list = cookieStore[host]; if (list === undefined) { - _cookies[host] = list = []; + cookieStore[host] = list = []; } list.push(cookie); }