Skip to content

Commit 5a883c4

Browse files
committed
Merge pull request #32 from Jakobud/feature/passable-options
Added Passable options
2 parents 14ab6ff + a164193 commit 5a883c4

File tree

2 files changed

+107
-18
lines changed

2 files changed

+107
-18
lines changed

README.md

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
ReconnectingWebSocket
22
=====================
33

4-
A small JavaScript library that decorates the WebSocket API to provide
5-
a WebSocket connection that will automatically reconnect if the
6-
connection is dropped.
4+
A small JavaScript library that decorates the WebSocket API to provide a WebSocket connection that will automatically reconnect if the connection is dropped.
75

86
It is API compatible, so when you have:
97

@@ -48,12 +46,57 @@ With a `ReconnectingWebSocket`, after an `onclose` event is called it will autom
4846

4947
This is all handled automatically for you by the library.
5048

51-
More
52-
----
49+
## Parameters
50+
51+
`var socket = new ReconnectingWebSocket(url, protocols, options);`
52+
53+
#### `url`
54+
- The URL you are connecting to.
55+
- http://dev.w3.org/html5/websockets/#the-websocket-interface
56+
57+
#### `protocols`
58+
- Optional string or array of protocols per the WebSocket spec.
59+
- [http://dev.w3.org/html5/websockets/#refsWSP
60+
61+
#### `options`
62+
- Options (see below)
63+
64+
## Options
65+
66+
Options can either be passed as the 3rd parameter upon instantiation or set directly on the object after instantiation:
67+
68+
`var socket = new ReconnectingWebSocket(url, null, {debug: true, reconnectInterval: 3000});`
69+
70+
or
71+
72+
var socket = new ReconnectingWebSocket(url);
73+
socket.debug = true;
74+
socket.timeoutInterval = 5400;
75+
76+
#### `debug`
77+
- Whether this instance should log debug messages or not. Debug messages are printed to `console.debug()`.
78+
- Accepts `true` or `false`
79+
- Default value: `false`
80+
81+
#### `reconnectInterval`
82+
- The number of milliseconds to delay before attempting to reconnect.
83+
- Accepts `integer`
84+
- Default: `1000`
85+
86+
####`reconnectDecay`
87+
- The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist.
88+
- Accepts `integer` or `float`
89+
- Default: `1.5`
90+
91+
#### `timeoutInterval`
92+
- The maximum time in milliseconds to wait for a connection to succeed before closing and retrying.
93+
- Accepts `integer`
94+
- Default: `2000`
95+
96+
---
5397

5498
Like this? Check out [websocketd](https://github.com/joewalnes/websocketd) for the simplest way to create WebSocket backends from any programming language.
5599

56100
[Follow @joewalnes](https://twitter.com/joewalnes)
57101

58102
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/joewalnes/reconnecting-websocket/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
59-

reconnecting-websocket.js

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,41 @@
5050
*
5151
* Latest version: https://github.com/joewalnes/reconnecting-websocket/
5252
* - Joe Walnes
53+
*
54+
* Syntax
55+
* ======
56+
* var socket = new ReconnectingWebSocket(url, protocols, options);
57+
*
58+
* Parameters
59+
* ==========
60+
* url - The url you are connecting to.
61+
* protocols - Optional string or array of protocols.
62+
* options - See below
63+
*
64+
* Options
65+
* =======
66+
* Options can either be passed upon instantiation or set after instantiation:
67+
*
68+
* var socket = new ReconnectingWebSocket(url, null, { debug: true, reconnectInterval: 4000 });
69+
*
70+
* or
71+
*
72+
* var socket = new ReconnectingWebSocket(url);
73+
* socket.debug = true;
74+
* socket.reconnectInterval = 4000;
75+
*
76+
* debug
77+
* - Whether this instance should log debug messages. Accepts true or false. Default: false.
78+
*
79+
* reconnectInterval
80+
* - The number of milliseconds to delay before attempting to reconnect. Accepts integer. Default: 1000.
81+
*
82+
* reconnectDecay
83+
* - The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. Accepts integer or float. Default: 1.5.
84+
*
85+
* timeoutInterval
86+
* - The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. Accepts integer. Default: 2000.
87+
*
5388
*/
5489
(function (global, factory) {
5590
if (typeof define === 'function' && define.amd) {
@@ -61,24 +96,35 @@
6196
}
6297
})(this, function () {
6398

64-
function ReconnectingWebSocket(url, protocols) {
65-
// These can be altered by calling code.
99+
function ReconnectingWebSocket(url, protocols, options) {
66100

67-
/** Whether this instance should log debug messages. */
68-
this.debug = false;
69-
/** The number of milliseconds to delay before attempting to reconnect. */
70-
this.reconnectInterval = 1000;
71-
/** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */
72-
this.reconnectDecay = 1.5;
73-
/** The number of attempted reconnects since starting, or the last successful connection. */
74-
this.reconnectAttempts = 0;
75-
/** The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. */
76-
this.timeoutInterval = 2000;
101+
// Default settings
102+
var settings = {
103+
/** Whether this instance should log debug messages. */
104+
debug: false,
105+
/** The number of milliseconds to delay before attempting to reconnect. */
106+
reconnectInterval: 1000,
107+
/** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */
108+
reconnectDecay: 1.5,
109+
/** The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. */
110+
timeoutInterval: 2000
111+
}
112+
113+
// Overwrite and define settings with options if they exist.
114+
for (var key in settings) {
115+
if (typeof options[key] !== 'undefined') {
116+
this[key] = options[key];
117+
} else {
118+
this[key] = settings[key];
119+
}
120+
}
77121

78122
// These should be treated as read-only properties
79123

80124
/** The URL as resolved by the constructor. This is always an absolute URL. Read only. */
81125
this.url = url;
126+
/** The number of attempted reconnects since starting, or the last successful connection. Read only. */
127+
this.reconnectAttempts = 0;
82128
/**
83129
* The current state of the connection.
84130
* Can be one of: WebSocket.CONNECTING, WebSocket.OPEN, WebSocket.CLOSING, WebSocket.CLOSED

0 commit comments

Comments
 (0)