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
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -515,7 +536,8 @@ angular.run(function ($websocket) {
reconnect: true,
reconnectInterval: 2000,
mock: false,
enqueue: false
enqueue: false,
forceNew: false
);
});
```
Expand Down Expand Up @@ -580,7 +602,8 @@ angular.run(function ($websocket) {
reconnectInterval: 2000,
enqueue: false,
mock: false,
protocols: ['binary', 'base64']
protocols: ['binary', 'base64'],
forceNew: false
});
});
```
Expand All @@ -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

Expand Down Expand Up @@ -1007,4 +1031,4 @@ And don't forget to make **pull requests**, damn it!

# License

Check out LICENSE file (MIT)
Check out LICENSE file (MIT)
11 changes: 10 additions & 1 deletion ng-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
reconnectInterval: 2000,
mock: false,
enqueue: false,
protocols: null
protocols: null,
forceNew: false
};

wsp.$setup = function (cfg) {
Expand Down Expand Up @@ -49,6 +50,10 @@
return wss.$$websocketList[url];
};

wss.$remove = function (url) {
delete wss.$$websocketList[url];
};

wss.$new = function (cfg) {
cfg = cfg || {};

Expand All @@ -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') {
Expand Down
20 changes: 19 additions & 1 deletion test/unit/ng-websocket-service-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
});
});
});

});