Skip to content

Commit ae8a19e

Browse files
committed
#4 Allow passing a function to the url parameter
1 parent 67d6a24 commit ae8a19e

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ WebSocket that will automatically reconnect if the connection is closed.
1717
- Handle connection timeouts
1818
- Full test coverage
1919
- Debug mode
20-
- Fast close (new in version 3)
20+
- Fast close
2121
- AMD build available (see dist folder)
22+
- Allows changing server URL
2223

2324
## Install
2425

@@ -61,6 +62,21 @@ rws.addEventListener('open', () => {
6162
});
6263
```
6364

65+
### Update URL
66+
67+
The `url` parameter also accepts a `function` so you have a chance to update the URL before connecting:
68+
```
69+
const ReconnectingWebSocket = require('reconnecting-websocket');
70+
71+
const urls = ['ws://my.site.com', 'ws://your.site.com', 'ws://their.site.com'];
72+
let urlIndex = 0;
73+
74+
// Round robin url provider
75+
const getUrl = () => urls[urlIndex++ % urls.length];
76+
77+
const rws = new ReconnectingWebSocket(getUrl);
78+
```
79+
6480
### Configure
6581

6682
#### Default options

dist/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
declare const ReconnectingWebsocket: (url: string, protocols?: string | string[], options?: {
1+
declare const ReconnectingWebsocket: (url: string | (() => string), protocols?: string | string[], options?: {
22
constructor?: new (url: string, protocols?: string | string[]) => WebSocket;
33
maxReconnectionDelay?: number;
44
minReconnectionDelay?: number;

dist/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ var ReconnectingWebsocket = function (url, protocols, options) {
114114
}
115115
log('connect');
116116
var oldWs = ws;
117-
ws = new config.constructor(url, protocols);
117+
var wsUrl = (typeof url === 'function') ? url() : url;
118+
ws = new config.constructor(wsUrl, protocols);
118119
connectingTimeout = setTimeout(function () {
119120
log('timeout');
120121
ws.close();

index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const reassignEventListeners = (ws: WebSocket, oldWs: WebSocket, listeners) => {
6363
};
6464

6565
const ReconnectingWebsocket = function(
66-
url: string,
66+
url: string | (() => string),
6767
protocols?: string|string[],
6868
options = <Options>{}
6969
) {
@@ -134,7 +134,8 @@ const ReconnectingWebsocket = function(
134134

135135
log('connect');
136136
const oldWs = ws;
137-
ws = new (<any>config.constructor)(url, protocols);
137+
const wsUrl = (typeof url === 'function') ? url() : url;
138+
ws = new (<any>config.constructor)(wsUrl, protocols);
138139

139140
connectingTimeout = setTimeout(() => {
140141
log('timeout');

test/test.js

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ test.cb('global WebSocket is used if available', t => {
3232
};
3333
});
3434

35+
test.cb('url provider', t => {
36+
t.plan(1);
37+
const wss = new WSS({port: PORT});
38+
39+
const getUrl = () => url;
40+
const ws = new RWS(getUrl, null, {maxRetries: 0, constructor: HWS});
41+
42+
ws.onopen = () => {
43+
t.pass('Connected');
44+
ws.close(1000, '', {keepClosed: true});
45+
wss.close();
46+
t.end();
47+
}
48+
});
49+
3550
test('connection status constants', t => {
3651
const ws = new RWS(url, null, {constructor: HWS});
3752
t.is(ws.CONNECTING, 0);

0 commit comments

Comments
 (0)