Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions ng-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
}
}
}
}
};
Expand Down Expand Up @@ -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;
};
Expand Down
83 changes: 83 additions & 0 deletions test/unit/ng-websocket-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -205,6 +241,43 @@ 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 noUpdate1 = true;
var noUpdate2 = true;
ws.$on({event: '$close', client: 'client1'}, function () {
noUpdate1 = false;
});
ws.$on({event: '$close', client: 'client2'}, function () {
noUpdate2 = false;
});

setTimeout(function () {
expect(noUpdate1).toBeTruthy();
expect(noUpdate2).toBeFalsy();
done();
}, 4000);

ws.$un({event: '$close', client: 'client1'});
});

it('should emit a custom event', function (done) {
ws.$emit('custom event', 'hello world');

Expand All @@ -214,6 +287,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 () {
Expand Down