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 successfully 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:
38 * onclose // lost connection
40 * onopen // sometime later...
45 * It is API compatible with the standard WebSocket API.
47 * Latest version: https://github.com/joewalnes/reconnecting-websocket/
50 (function (global, factory) {
51 if (typeof define === 'function' && define.amd) {
53 } else if (typeof module !== 'undefined' && module.exports){
54 module.exports = factory();
56 global.ReconnectingWebSocket = factory();
58 })(this, function () {
60 function ReconnectingWebSocket(url, protocols) {
61 protocols = protocols || [];
63 // These can be altered by calling code.
65 this.reconnectInterval = 1000;
66 this.reconnectDecay = 1.5;
67 this.reconnectAttempts = 0;
68 this.timeoutInterval = 2000;
72 var forcedClose = false;
76 this.protocols = protocols;
77 this.readyState = WebSocket.CONNECTING;
78 this.URL = url; // Public API
80 this.onopen = function(event) {
83 this.onclose = function(event) {
86 this.onconnecting = function(event) {
89 this.onmessage = function(event) {
92 this.onerror = function(event) {
95 function connect(reconnectAttempt) {
96 ws = new WebSocket(self.url, protocols);
101 if (self.debug || ReconnectingWebSocket.debugAll) {
102 console.debug('ReconnectingWebSocket', 'attempt-connect', url);
106 var timeout = setTimeout(function() {
107 if (self.debug || ReconnectingWebSocket.debugAll) {
108 console.debug('ReconnectingWebSocket', 'connection-timeout', url);
113 }, self.timeoutInterval);
115 ws.onopen = function(event) {
116 clearTimeout(timeout);
117 if (self.debug || ReconnectingWebSocket.debugAll) {
118 console.debug('ReconnectingWebSocket', 'onopen', url);
120 self.readyState = WebSocket.OPEN;
121 reconnectAttempt = false;
122 self.reconnectAttempts = 0;
126 ws.onclose = function(event) {
127 clearTimeout(timeout);
130 self.readyState = WebSocket.CLOSED;
133 self.readyState = WebSocket.CONNECTING;
135 if (!reconnectAttempt && !timedOut) {
136 if (self.debug || ReconnectingWebSocket.debugAll) {
137 console.debug('ReconnectingWebSocket', 'onclose', url);
141 setTimeout(function() {
142 self.reconnectAttempts++;
144 }, self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts));
147 ws.onmessage = function(event) {
148 if (self.debug || ReconnectingWebSocket.debugAll) {
149 console.debug('ReconnectingWebSocket', 'onmessage', url, event.data);
151 self.onmessage(event);
153 ws.onerror = function(event) {
154 if (self.debug || ReconnectingWebSocket.debugAll) {
155 console.debug('ReconnectingWebSocket', 'onerror', url, event);
162 this.send = function(data) {
164 if (self.debug || ReconnectingWebSocket.debugAll) {
165 console.debug('ReconnectingWebSocket', 'send', url, data);
167 return ws.send(data);
169 throw 'INVALID_STATE_ERR : Pausing to reconnect websocket';
173 this.close = function() {
181 * Additional public API method to refresh the connection if still open (close, re-open).
182 * For example, if the app suspects bad data / missed heart beats, it can try to refresh.
184 this.refresh = function() {
192 * Setting this to true is the equivalent of setting all instances of ReconnectingWebSocket.debug to true.
194 ReconnectingWebSocket.debugAll = false;
196 return ReconnectingWebSocket;