~bcsaller/junk/juju-ui-brunch

« back to all changes in this revision

Viewing changes to vendor/scripts/reconnecting-websocket.js

  • Committer: Benjamin Saller
  • Date: 2012-07-13 07:10:02 UTC
  • Revision ID: bcsaller@gmail.com-20120713071002-x4teg6mrogmx1972
initial checkin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// MIT License:
 
2
//
 
3
// Copyright (c) 2010-2012, Joe Walnes
 
4
//
 
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:
 
11
//
 
12
// The above copyright notice and this permission notice shall be included in
 
13
// all copies or substantial portions of the Software.
 
14
//
 
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
 
21
// THE SOFTWARE.
 
22
 
 
23
/**
 
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
 
26
 * again.
 
27
 *
 
28
 * It is API compatible, so when you have:
 
29
 *   ws = new WebSocket('ws://....');
 
30
 * you can replace with:
 
31
 *   ws = new ReconnectingWebSocket('ws://....');
 
32
 *
 
33
 * The event stream will typically look like:
 
34
 *  onopen
 
35
 *  onmessage
 
36
 *  onmessage
 
37
 *  onclose // lost connection
 
38
 *  onopen  // sometime later...
 
39
 *  onmessage
 
40
 *  onmessage
 
41
 *  etc... 
 
42
 *
 
43
 * It is API compatible with the standard WebSocket API.
 
44
 *
 
45
 * - Joe Walnes
 
46
 */
 
47
function ReconnectingWebSocket(url, protocols) {
 
48
 
 
49
    // These can be altered by calling code.
 
50
    this.debug = false;
 
51
    this.reconnectInterval = 1000;
 
52
    this.timeoutInterval = 2000;
 
53
 
 
54
    var self = this;
 
55
    var ws;
 
56
    var forcedClose = false;
 
57
    var timedOut = false;
 
58
    
 
59
    this.url = url;
 
60
    this.protocols = protocols;
 
61
    this.readyState = WebSocket.CONNECTING;
 
62
    this.URL = url; // Public API
 
63
 
 
64
    this.onopen = function(event) {
 
65
    };
 
66
 
 
67
    this.onclose = function(event) {
 
68
    };
 
69
 
 
70
    this.onmessage = function(event) {
 
71
    };
 
72
 
 
73
    this.onerror = function(event) {
 
74
    };
 
75
 
 
76
    function connect(reconnectAttempt) {
 
77
        ws = new WebSocket(url, protocols);
 
78
        if (self.debug || ReconnectingWebSocket.debugAll) {
 
79
            console.debug('ReconnectingWebSocket', 'attempt-connect', url);
 
80
        }
 
81
        
 
82
        var localWs = ws;
 
83
        var timeout = setTimeout(function() {
 
84
            if (self.debug || ReconnectingWebSocket.debugAll) {
 
85
                console.debug('ReconnectingWebSocket', 'connection-timeout', url);
 
86
            }
 
87
            timedOut = true;
 
88
            localWs.close();
 
89
            timedOut = false;
 
90
        }, self.timeoutInterval);
 
91
        
 
92
        ws.onopen = function(event) {
 
93
            clearTimeout(timeout);
 
94
            if (self.debug || ReconnectingWebSocket.debugAll) {
 
95
                console.debug('ReconnectingWebSocket', 'onopen', url);
 
96
            }
 
97
            self.readyState = WebSocket.OPEN;
 
98
            reconnectAttempt = false;
 
99
            self.onopen(event);
 
100
        };
 
101
        
 
102
        ws.onclose = function(event) {
 
103
            clearTimeout(timeout);
 
104
            ws = null;
 
105
            if (forcedClose) {
 
106
                self.readyState = WebSocket.CLOSED;
 
107
                self.onclose(event);
 
108
            } else {
 
109
                self.readyState = WebSocket.CONNECTING;
 
110
                if (!reconnectAttempt && !timedOut) {
 
111
                    if (self.debug || ReconnectingWebSocket.debugAll) {
 
112
                        console.debug('ReconnectingWebSocket', 'onclose', url);
 
113
                    }
 
114
                    self.onclose(event);
 
115
                }
 
116
                setTimeout(function() {
 
117
                    connect(true);
 
118
                }, self.reconnectInterval);
 
119
            }
 
120
        };
 
121
        ws.onmessage = function(event) {
 
122
            if (self.debug || ReconnectingWebSocket.debugAll) {
 
123
                console.debug('ReconnectingWebSocket', 'onmessage', url, event.data);
 
124
            }
 
125
                self.onmessage(event);
 
126
        };
 
127
        ws.onerror = function(event) {
 
128
            if (self.debug || ReconnectingWebSocket.debugAll) {
 
129
                console.debug('ReconnectingWebSocket', 'onerror', url, event);
 
130
            }
 
131
            self.onerror(event);
 
132
        };
 
133
    }
 
134
    connect(url);
 
135
 
 
136
    this.send = function(data) {
 
137
        if (ws) {
 
138
            if (self.debug || ReconnectingWebSocket.debugAll) {
 
139
                console.debug('ReconnectingWebSocket', 'send', url, data);
 
140
            }
 
141
            return ws.send(data);
 
142
        } else {
 
143
            throw 'INVALID_STATE_ERR : Pausing to reconnect websocket';
 
144
        }
 
145
    };
 
146
 
 
147
    this.close = function() {
 
148
        if (ws) {
 
149
            forcedClose = true;
 
150
            ws.close();
 
151
        }
 
152
    };
 
153
 
 
154
    /**
 
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.
 
157
     */
 
158
    this.refresh = function() {
 
159
        if (ws) {
 
160
            ws.close();
 
161
        }
 
162
    };
 
163
}
 
164
 
 
165
/**
 
166
 * Setting this to true is the equivalent of setting all instances of ReconnectingWebSocket.debug to true.
 
167
 */
 
168
ReconnectingWebSocket.debugAll = false;
 
169