diff --git a/README.md b/README.md index 946bea4..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) @@ -391,6 +392,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: @@ -515,7 +536,8 @@ angular.run(function ($websocket) { reconnect: true, reconnectInterval: 2000, mock: false, - enqueue: false + enqueue: false, + forceNew: false ); }); ``` @@ -580,7 +602,8 @@ angular.run(function ($websocket) { reconnectInterval: 2000, enqueue: false, mock: false, - protocols: ['binary', 'base64'] + protocols: ['binary', 'base64'], + forceNew: false }); }); ``` @@ -593,6 +616,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-new) ### Constants @@ -1007,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) diff --git a/ng-websocket.js b/ng-websocket.js index ca65608..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) { @@ -49,6 +50,10 @@ return wss.$$websocketList[url]; }; + wss.$remove = function (url) { + delete wss.$$websocketList[url]; + }; + wss.$new = function (cfg) { cfg = cfg || {}; @@ -63,6 +68,10 @@ } } + if (cfg.forceNew) { + wss.$remove(cfg.url); + } + // If the websocket already exists, return that instance var ws = wss.$get(cfg.url); if (typeof ws === 'undefined') { 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 + +});