3
// Copyright (c) 2010-2012, Joe Walnes
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
12
// The above copyright notice and this permission notice shall be included in
13
// all copies or substantial portions of the Software.
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
* This behaves like a WebSocket in every way, except if it fails to connect,
25
* or it gets disconnected, it will repeatedly poll until it succesfully connects
28
* It is API compatible, so when you have:
29
* ws = new WebSocket('ws://....');
30
* you can replace with:
31
* ws = new ReconnectingWebSocket('ws://....');
33
* The event stream will typically look like:
37
* onclose // lost connection
38
* onopen // sometime later...
43
* It is API compatible with the standard WebSocket API.
47
function ReconnectingWebSocket(url, protocols) {
49
// These can be altered by calling code.
51
this.reconnectInterval = 1000;
52
this.timeoutInterval = 2000;
56
var forcedClose = false;
60
this.protocols = protocols;
61
this.readyState = WebSocket.CONNECTING;
62
this.URL = url; // Public API
64
this.onopen = function(event) {
67
this.onclose = function(event) {
70
this.onmessage = function(event) {
73
this.onerror = function(event) {
76
function connect(reconnectAttempt) {
77
ws = new WebSocket(url, protocols);
78
if (self.debug || ReconnectingWebSocket.debugAll) {
79
console.debug('ReconnectingWebSocket', 'attempt-connect', url);
83
var timeout = setTimeout(function() {
84
if (self.debug || ReconnectingWebSocket.debugAll) {
85
console.debug('ReconnectingWebSocket', 'connection-timeout', url);
90
}, self.timeoutInterval);
92
ws.onopen = function(event) {
93
clearTimeout(timeout);
94
if (self.debug || ReconnectingWebSocket.debugAll) {
95
console.debug('ReconnectingWebSocket', 'onopen', url);
97
self.readyState = WebSocket.OPEN;
98
reconnectAttempt = false;
102
ws.onclose = function(event) {
103
clearTimeout(timeout);
106
self.readyState = WebSocket.CLOSED;
109
self.readyState = WebSocket.CONNECTING;
110
if (!reconnectAttempt && !timedOut) {
111
if (self.debug || ReconnectingWebSocket.debugAll) {
112
console.debug('ReconnectingWebSocket', 'onclose', url);
116
setTimeout(function() {
118
}, self.reconnectInterval);
121
ws.onmessage = function(event) {
122
if (self.debug || ReconnectingWebSocket.debugAll) {
123
console.debug('ReconnectingWebSocket', 'onmessage', url, event.data);
125
self.onmessage(event);
127
ws.onerror = function(event) {
128
if (self.debug || ReconnectingWebSocket.debugAll) {
129
console.debug('ReconnectingWebSocket', 'onerror', url, event);
136
this.send = function(data) {
138
if (self.debug || ReconnectingWebSocket.debugAll) {
139
console.debug('ReconnectingWebSocket', 'send', url, data);
141
return ws.send(data);
143
throw 'INVALID_STATE_ERR : Pausing to reconnect websocket';
147
this.close = function() {
155
* Additional public API method to refresh the connection if still open (close, re-open).
156
* For example, if the app suspects bad data / missed heart beats, it can try to refresh.
158
this.refresh = function() {
166
* Setting this to true is the equivalent of setting all instances of ReconnectingWebSocket.debug to true.
168
ReconnectingWebSocket.debugAll = false;