From 31d5ef20aacaa125e29f283f83affd930dd1ea92 Mon Sep 17 00:00:00 2001 From: Cyril DUPETIT Date: Tue, 10 Feb 2015 15:35:05 +0100 Subject: [PATCH 1/2] Add Client --- ng-websocket.js | 41 +++++++++++------ test/unit/ng-websocket-spec.js | 81 ++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 14 deletions(-) diff --git a/ng-websocket.js b/ng-websocket.js index ca65608..b57bbd0 100644 --- a/ng-websocket.js +++ b/ng-websocket.js @@ -108,12 +108,17 @@ Array.prototype.push.apply(args, arguments); - var event = args.shift(), - handlers = me.$$eventMap[event]; - - if (typeof handlers !== 'undefined') { - for (var i = 0; i < handlers.length; i++) { - if (typeof handlers[i] === 'function') handlers[i].apply(me, args); + var event = args.shift(), + clients = me.$$eventMap[event]; + + if (typeof clients !== 'undefined') { + for (var client in clients) { + var handlers = clients[client]; + if (typeof handlers !== 'undefined') { + for (var i = 0; i < handlers.length; i++) { + if (typeof handlers[i] === 'function') { handlers[i].apply(me, args); } + } + } } } }; @@ -195,21 +200,29 @@ Array.prototype.push.apply(handlers, arguments); - var event = handlers.shift(); - if (typeof event !== 'string' || handlers.length === 0) throw new Error('$on accept two parameters at least: a String and a Function or an array of Functions'); - - me.$$eventMap[event] = me.$$eventMap[event] || []; - for (var i = 0; i < handlers.length; i++) { - me.$$eventMap[event].push(handlers[i]); + var cfg = handlers.shift(); + cfg = cfg || {}; + if (typeof cfg === 'string') cfg = { event: cfg, }; + if (typeof cfg.client === 'undefined') cfg.client = '$websocket'; + if (typeof cfg.event !== 'string' || handlers.length === 0) throw new Error('$on accept two parameters at least: a String and a Function or an array of Functions'); + + me.$$eventMap[cfg.event] = me.$$eventMap[cfg.event] || []; + me.$$eventMap[cfg.event][cfg.client] = me.$$eventMap[cfg.event][cfg.client] || []; + for (var i = 0; i < handlers.length; i++) + { + me.$$eventMap[cfg.event][cfg.client].push(handlers[i]); } return me; }; me.$un = function (event) { - if (typeof event !== 'string') throw new Error('$un needs a String representing an event.'); + cfg = event || {}; + if (typeof cfg === 'string') cfg = { event: cfg, }; + if (typeof cfg.client === 'undefined') cfg.client = '$websocket'; + if (typeof cfg.event !== 'string') throw new Error('$un needs a String representing an event.'); - if (typeof me.$$eventMap[event] !== 'undefined') delete me.$$eventMap[event]; + if (typeof me.$$eventMap[cfg.event][cfg.client] !== 'undefined') delete me.$$eventMap[cfg.event][cfg.client]; return me; }; diff --git a/test/unit/ng-websocket-spec.js b/test/unit/ng-websocket-spec.js index 82eb58c..189918e 100644 --- a/test/unit/ng-websocket-spec.js +++ b/test/unit/ng-websocket-spec.js @@ -163,6 +163,16 @@ describe('Testing ng-websocket', function () { }); }); + it('should set a listener for a client on $close general event', function (done) { + ws.$close(); + + ws.$on({event: '$close', client: 'client'}, function () { + expect(ws.$status()).toEqual(ws.$CLOSED); + + done(); + }); + }); + it('should set a list of listeners on $close general event', function (done) { var listeners = 3, counter = 0; @@ -189,6 +199,32 @@ describe('Testing ng-websocket', function () { }); }); + it('should set a list of listeners for a client on $close general event', function (done) { + var listeners = 3, + counter = 0; + + ws.$close(); + + ws.$on({event: '$close', client: 'client'}, function () { + expect(ws.$status()).toEqual(ws.$CLOSED); + + counter++; + }) + .$on({event: '$close', client: 'client'}, function () { + expect(ws.$status()).toEqual(ws.$CLOSED); + + counter++; + }) + .$on({event: '$close', client: 'client'}, function () { + expect(ws.$status()).toEqual(ws.$CLOSED); + + counter++; + + expect(counter).toEqual(listeners); + done(); + }); + }); + it('should unset a listener on $close general event', function (done) { ws.$close(); @@ -205,6 +241,41 @@ describe('Testing ng-websocket', function () { ws.$un('$close'); }); + it('should unset a listener for a client on $close general event', function (done) { + ws.$close(); + + var noUpdate = true; + ws.$on({event: '$close', client: 'client1'}, function () { + noUpdate = false; + }); + + setTimeout(function () { + expect(noUpdate).toBeTruthy(); + done(); + }, 4000); + + ws.$un({event: '$close', client: 'client1'}); + }); + + it('should unset a listener for a client and keep a listener for another client on $close general event', function (done) { + ws.$close(); + + var noUpdate = true; + ws.$on({event: '$close', client: 'client1'}, function () { + noUpdate = false; + }); + ws.$on({event: '$close', client: 'client2'}, function () { + noUpdate = false; + }); + + setTimeout(function () { + expect(noUpdate).toBeFalsy(); + done(); + }, 4000); + + ws.$un({event: '$close', client: 'client1'}); + }); + it('should emit a custom event', function (done) { ws.$emit('custom event', 'hello world'); @@ -214,6 +285,16 @@ describe('Testing ng-websocket', function () { done(); }); }); + + it('should emit a custom event and a client receive it', function (done) { + ws.$emit('custom event', 'hello world'); + + ws.$on({event: 'custom event', client: 'client1'}, function (message) { + expect(message).toEqual('hello world'); + + done(); + }); + }); }); describe('Testing a lazy websocket', function () { From 1bd0512539e531df2695bfda4bd1761ad37279ce Mon Sep 17 00:00:00 2001 From: Cyril DUPETIT Date: Tue, 10 Feb 2015 15:40:33 +0100 Subject: [PATCH 2/2] Add Client --- test/unit/ng-websocket-spec.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/unit/ng-websocket-spec.js b/test/unit/ng-websocket-spec.js index 189918e..b20bbc3 100644 --- a/test/unit/ng-websocket-spec.js +++ b/test/unit/ng-websocket-spec.js @@ -260,16 +260,18 @@ describe('Testing ng-websocket', function () { it('should unset a listener for a client and keep a listener for another client on $close general event', function (done) { ws.$close(); - var noUpdate = true; + var noUpdate1 = true; + var noUpdate2 = true; ws.$on({event: '$close', client: 'client1'}, function () { - noUpdate = false; + noUpdate1 = false; }); ws.$on({event: '$close', client: 'client2'}, function () { - noUpdate = false; + noUpdate2 = false; }); setTimeout(function () { - expect(noUpdate).toBeFalsy(); + expect(noUpdate1).toBeTruthy(); + expect(noUpdate2).toBeFalsy(); done(); }, 4000);