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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ With a `ReconnectingWebSocket`, after an `onclose` event is called it will autom

This is all handled automatically for you by the library.

Manually Opening, Closing and Refreshing connections
----------------------------------------

When you create a new `ReconnectingWebSocket`, the `WebSocket` connection is created and opened for you. In the case that you need to manually close a connection, simply run the `close()` method. To open the connection back up, use `open()`. If you need to close and immediately reopen the connection, use `refresh()`.

More
----

Expand Down
18 changes: 9 additions & 9 deletions reconnecting-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* onopen // sometime later...
* onmessage
* onmessage
* etc...
* etc...
*
* It is API compatible with the standard WebSocket API.
*
Expand Down Expand Up @@ -71,7 +71,7 @@
var ws;
var forcedClose = false;
var timedOut = false;

this.url = url;
this.protocols = protocols;
this.readyState = WebSocket.CONNECTING;
Expand All @@ -92,14 +92,14 @@
this.onerror = function(event) {
};

function connect(reconnectAttempt) {
this.open = function(reconnectAttempt) {
ws = new WebSocket(url, protocols);

self.onconnecting();
if (self.debug || ReconnectingWebSocket.debugAll) {
console.debug('ReconnectingWebSocket', 'attempt-connect', url);
}

var localWs = ws;
var timeout = setTimeout(function() {
if (self.debug || ReconnectingWebSocket.debugAll) {
Expand All @@ -109,7 +109,7 @@
localWs.close();
timedOut = false;
}, self.timeoutInterval);

ws.onopen = function(event) {
clearTimeout(timeout);
if (self.debug || ReconnectingWebSocket.debugAll) {
Expand All @@ -120,7 +120,7 @@
self.reconnectAttempts = 0;
self.onopen(event);
};

ws.onclose = function(event) {
clearTimeout(timeout);
ws = null;
Expand All @@ -138,7 +138,7 @@
}
setTimeout(function() {
self.reconnectAttempts++;
connect(true);
self.open(true);
}, self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts));
}
};
Expand All @@ -155,7 +155,7 @@
self.onerror(event);
};
}
connect(false);
this.open(false);

this.send = function(data) {
if (ws) {
Expand Down