From 823c6fb5c18dc7d37e99667f29234b4e40748b11 Mon Sep 17 00:00:00 2001 From: go-ive Date: Thu, 28 May 2015 23:13:51 +0200 Subject: [PATCH 1/6] force new connection on demand --- README.md | 12 ++++++++++++ ng-websocket.js | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/README.md b/README.md index 946bea4..1f40ef8 100644 --- a/README.md +++ b/README.md @@ -502,6 +502,18 @@ angular.run(function ($websocket) { ``` A new instance is returned and the internal WebSocket has already started the connection with the websocket server on the backend. +By default, once created connections will be reused. If you want to force the creation of a new connection, use forceNewConnection: + +```javascript +angular.run(function ($websocket) { + var ws = $websocket.$new({ + url: 'ws://localhost:12345'), + forceNewConnection: true + }); +}); +``` + +Bear in mind that the previous websocket will not close, so be careful with this parameter. **object** diff --git a/ng-websocket.js b/ng-websocket.js index ca65608..8f2a686 100644 --- a/ng-websocket.js +++ b/ng-websocket.js @@ -49,6 +49,10 @@ return wss.$$websocketList[url]; }; + wss.$remove = function (url) { + delete wss.$$websocketList[url]; + }; + wss.$new = function (cfg) { cfg = cfg || {}; @@ -61,6 +65,15 @@ if (typeof arguments[1] === 'string' && arguments[1].length > 0) cfg.protocols = [arguments[1]]; else if (typeof arguments[1] === 'object' && arguments[1].length > 0) cfg.protocols = arguments[1]; } + } else if(typeof cfg === 'object') { + if (cfg.forceNewConnection) { + wss.$remove(cfg.url); + } + + cfg = { + url: cfg.url, + forceNewConnection: cfg.forceNewConnection ? cfg.forceNewConnection : false + }; } // If the websocket already exists, return that instance From 8052bf63e094b8a0cce14c423c0e78e632b2ed10 Mon Sep 17 00:00:00 2001 From: go-ive Date: Thu, 28 May 2015 23:35:41 +0200 Subject: [PATCH 2/6] more concise, better description --- README.md | 39 +++++++++++++++++++++++++-------------- ng-websocket.js | 14 +++++--------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 1f40ef8..1465043 100644 --- a/README.md +++ b/README.md @@ -391,6 +391,26 @@ angular.run(function ($websocket) { **Default: disabled** +## Force New + +In some cases you may not want to reuse an existing websocket connection and need to create a new one. +By default, creating a new websocket with the same url will try to reuse a before created websocket connection, even if it is closed. +You can force a new connection using this flag. + +```javascript +angular.run(function ($websocket) { + var ws = $websocket.$new({ + url: 'ws://localhost:12345', + forceNew: true + }); +}); +``` + +Bear in mind that this will open a new connection to the same websocket, no matter if one exists already. Make sure to close +your old websocket correctly and timely. + +** Default: false ** + # Testing This module uses [Karma](http://karma-runner.github.io/0.12/index.html) with [Jasmine](http://jasmine.github.io/) for unit testing, so before launching any test check out if all dependencies are correctly installed: @@ -502,18 +522,6 @@ angular.run(function ($websocket) { ``` A new instance is returned and the internal WebSocket has already started the connection with the websocket server on the backend. -By default, once created connections will be reused. If you want to force the creation of a new connection, use forceNewConnection: - -```javascript -angular.run(function ($websocket) { - var ws = $websocket.$new({ - url: 'ws://localhost:12345'), - forceNewConnection: true - }); -}); -``` - -Bear in mind that the previous websocket will not close, so be careful with this parameter. **object** @@ -527,7 +535,8 @@ angular.run(function ($websocket) { reconnect: true, reconnectInterval: 2000, mock: false, - enqueue: false + enqueue: false, + forceNew: false ); }); ``` @@ -592,7 +601,8 @@ angular.run(function ($websocket) { reconnectInterval: 2000, enqueue: false, mock: false, - protocols: ['binary', 'base64'] + protocols: ['binary', 'base64'], + forceNew: false }); }); ``` @@ -605,6 +615,7 @@ Following the explanation of the configuration object - {Type} PropertyName (def - **{Boolean} enqueue (false)**: enqueue unsent messages. By default, a websocket discards messages when the connection is closed (false) but it can enqueue them and send afterwards the connection gets open back (true). For more information see [Features - Enqueue Unsent Messages](#enqueue) - **{Boolean/Object} mock (false)**: mock a websocket server. By default, a websocket run only if the webserver socket is listening (false) but it can be useful to mock the backend to make the websocket working (true). For more information see [Features - Mock Websocket Server](#mock) - **{String/String[]} (null)**: Either a single protocol string or an array of protocol strings. This is the same as the WebSocket protocols argument. + - **{Boolean} forceNew (false)**: force creation of new connection. By default, a websocket is reused on [$websocket.$new](#new). For more information see [Features - Force New](#force) ### Constants diff --git a/ng-websocket.js b/ng-websocket.js index 8f2a686..31d38d9 100644 --- a/ng-websocket.js +++ b/ng-websocket.js @@ -17,7 +17,8 @@ reconnectInterval: 2000, mock: false, enqueue: false, - protocols: null + protocols: null, + forceNew: false }; wsp.$setup = function (cfg) { @@ -65,15 +66,10 @@ if (typeof arguments[1] === 'string' && arguments[1].length > 0) cfg.protocols = [arguments[1]]; else if (typeof arguments[1] === 'object' && arguments[1].length > 0) cfg.protocols = arguments[1]; } - } else if(typeof cfg === 'object') { - if (cfg.forceNewConnection) { - wss.$remove(cfg.url); - } + } - cfg = { - url: cfg.url, - forceNewConnection: cfg.forceNewConnection ? cfg.forceNewConnection : false - }; + if (cfg.forceNew) { + wss.$remove(cfg.url); } // If the websocket already exists, return that instance From b9011e5f09d7f301370fbce6fcf3509529866831 Mon Sep 17 00:00:00 2001 From: go-ive Date: Thu, 28 May 2015 23:36:24 +0200 Subject: [PATCH 3/6] anchor fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1465043..b03f1aa 100644 --- a/README.md +++ b/README.md @@ -615,7 +615,7 @@ Following the explanation of the configuration object - {Type} PropertyName (def - **{Boolean} enqueue (false)**: enqueue unsent messages. By default, a websocket discards messages when the connection is closed (false) but it can enqueue them and send afterwards the connection gets open back (true). For more information see [Features - Enqueue Unsent Messages](#enqueue) - **{Boolean/Object} mock (false)**: mock a websocket server. By default, a websocket run only if the webserver socket is listening (false) but it can be useful to mock the backend to make the websocket working (true). For more information see [Features - Mock Websocket Server](#mock) - **{String/String[]} (null)**: Either a single protocol string or an array of protocol strings. This is the same as the WebSocket protocols argument. - - **{Boolean} forceNew (false)**: force creation of new connection. By default, a websocket is reused on [$websocket.$new](#new). For more information see [Features - Force New](#force) + - **{Boolean} forceNew (false)**: force creation of new connection. By default, a websocket is reused on [$websocket.$new](#new). For more information see [Features - Force New](#force-new) ### Constants From 07918118cd2ddf0bddee63017ae26957e96d148f Mon Sep 17 00:00:00 2001 From: go-ive Date: Thu, 28 May 2015 23:37:11 +0200 Subject: [PATCH 4/6] bold --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b03f1aa..e6ac703 100644 --- a/README.md +++ b/README.md @@ -409,7 +409,7 @@ angular.run(function ($websocket) { Bear in mind that this will open a new connection to the same websocket, no matter if one exists already. Make sure to close your old websocket correctly and timely. -** Default: false ** +**Default: false** # Testing From 60aea9df91b8a7bd43464471d7337498625989e7 Mon Sep 17 00:00:00 2001 From: Ivan Antes-Klobucar Date: Fri, 29 May 2015 10:48:25 +0200 Subject: [PATCH 5/6] added force new to menu --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e6ac703..d136217 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ ng-websocket - [Auto Reconnection](#reconnect) - [Enqueue Unsent Messages](#enqueue) - [Mock Websocket Server](#mock) + - [Force New Connection](#force-new) - [Testing](#testing) - [API](#api) - [$websocketProvider](#websocketProvider) @@ -1030,4 +1031,4 @@ And don't forget to make **pull requests**, damn it! # License -Check out LICENSE file (MIT) \ No newline at end of file +Check out LICENSE file (MIT) From e787c3dafccb0a4ef085c93bc2b43e4ea4aa0acc Mon Sep 17 00:00:00 2001 From: ive Date: Fri, 29 May 2015 11:34:57 +0200 Subject: [PATCH 6/6] added test for forceNew --- test/unit/ng-websocket-service-spec.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/unit/ng-websocket-service-spec.js b/test/unit/ng-websocket-service-spec.js index 30902ef..7f40740 100644 --- a/test/unit/ng-websocket-service-spec.js +++ b/test/unit/ng-websocket-service-spec.js @@ -13,6 +13,9 @@ describe('Testing ng-websocket-service', function () { expect($websocket.$new).toBeDefined(); expect($websocket.$get).toBeDefined(); }); + it('should have $remove method', function(){ + expect($websocket.$remove).toBeDefined(); + }); }); describe('Testing $new operator', function () { @@ -82,5 +85,20 @@ describe('Testing ng-websocket-service', function () { expect(wsObj).not.toEqual(ws); }); + + it('should return a new ng-websocket instance for forceNew', function(){ + var url = 'ws://localhost:12345'; + var ws1 = $websocket.$new({ + url: url + }); + + var ws2 = $websocket.$new({ + url: url, + forceNew: true + }); + + expect(ws1).not.toEqual(ws2); + }); }); -}); \ No newline at end of file + +});