From 9b76587536457f1d2bfb17d7fe2592694315714d Mon Sep 17 00:00:00 2001 From: Vaclav Chalupa Date: Wed, 29 Jul 2015 21:36:58 +0200 Subject: [PATCH] Add support for CloseEvent --- README.md | 11 +++++++++-- ng-websocket.js | 12 ++++++------ test/unit/ng-websocket-spec.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 946bea4..e09dd79 100644 --- a/README.md +++ b/README.md @@ -609,7 +609,7 @@ There are custom events fired by ngWebsocket. They are useful to setup a listener for certain situations and behaviours: - **$open**: the websocket gets open - - **$close**: the websocket gets closed + - **$close**: the websocket gets closed (callback params: {CloseEvent} closeEvent) - **$error**: an error occurred (callback params: {Error} error) - **$message**: the original message sent from the server (callback params: {String} message). Usually, it's a JSON encoded string containing the event to fire and the data to pass ({"event": "an event", "data": "some data"}) @@ -784,9 +784,16 @@ angular.run(function ($websocket) { **Usage** ```javascript -$close() +$close(code, reason) ``` +**Arguments** + +| **Param** | **Type** | **Details** | +| --------- | -------- | ----------- | +| code | Number | status code, optional | +| reason | String | explanation, optional | + **Returns** | **Type** | **Details** | diff --git a/ng-websocket.js b/ng-websocket.js index ca65608..4c480b4 100644 --- a/ng-websocket.js +++ b/ng-websocket.js @@ -163,7 +163,7 @@ me.$$fireEvent('$open'); }; - me.$$ws.onclose = function () { + me.$$ws.onclose = function (closeEvent) { // Activate the reconnect task if (me.$$config.reconnect) { me.$$reconnectTask = setInterval(function () { @@ -171,7 +171,7 @@ }, me.$$config.reconnectInterval); } - me.$$fireEvent('$close'); + me.$$fireEvent('$close', closeEvent); }; return me; @@ -239,8 +239,8 @@ return me; }; - me.$close = function () { - if (me.$status() !== me.$CLOSED) me.$$ws.close(); + me.$close = function (code, reason) { + if (me.$status() !== me.$CLOSED) me.$$ws.close(code, reason); if (me.$$reconnectTask) { clearInterval(me.$$reconnectTask); @@ -299,14 +299,14 @@ else throw new Error('WebSocket is already in CLOSING or CLOSED state.'); }; - me.close = function () { + me.close = function (code, reason) { if (me.readyState === me.OPEN) { me.readyState = me.CLOSING; setTimeout(function () { me.readyState = me.CLOSED; - me.onclose(); + me.onclose({code: code, reason: reason}); // mock CloseEvent object }, closeTimeout); } diff --git a/test/unit/ng-websocket-spec.js b/test/unit/ng-websocket-spec.js index 82eb58c..7aa4f83 100644 --- a/test/unit/ng-websocket-spec.js +++ b/test/unit/ng-websocket-spec.js @@ -135,6 +135,40 @@ describe('Testing ng-websocket', function () { }); }); + describe('Testing close code, reason and CloseEvent', function () { + var ws; + + beforeEach(function (done) { + ws = $websocket.$new({ + url: 'ws://localhost:12345', + mock: true + }); + + ws.$on('$open', function () { + done(); + }); + }); + + afterEach(function () { + ws.$close(); + }); + + it('should receive a CloseEvent on $close event', function (done) { + var code = 5000; + var reason = 'close reason'; + + ws.$close(code, reason); + + ws.$on('$close', function (closeEvent) { + expect(closeEvent).not.toBeUndefined(); + expect(closeEvent.code).toEqual(code); + expect(closeEvent.reason).toEqual(reason); + + done(); + }); + }); + }); + describe('Testing $on, $un, $emit functions', function () { var ws;