Initial commit
[yaffs-website] / node_modules / livereload-js / lib / connector.js
1 (function() {
2   var Connector, PROTOCOL_6, PROTOCOL_7, Parser, Version, _ref;
3
4   _ref = require('./protocol'), Parser = _ref.Parser, PROTOCOL_6 = _ref.PROTOCOL_6, PROTOCOL_7 = _ref.PROTOCOL_7;
5
6   Version = '2.2.2';
7
8   exports.Connector = Connector = (function() {
9     function Connector(options, WebSocket, Timer, handlers) {
10       this.options = options;
11       this.WebSocket = WebSocket;
12       this.Timer = Timer;
13       this.handlers = handlers;
14       this._uri = "ws" + (this.options.https ? "s" : "") + "://" + this.options.host + ":" + this.options.port + "/livereload";
15       this._nextDelay = this.options.mindelay;
16       this._connectionDesired = false;
17       this.protocol = 0;
18       this.protocolParser = new Parser({
19         connected: (function(_this) {
20           return function(protocol) {
21             _this.protocol = protocol;
22             _this._handshakeTimeout.stop();
23             _this._nextDelay = _this.options.mindelay;
24             _this._disconnectionReason = 'broken';
25             return _this.handlers.connected(protocol);
26           };
27         })(this),
28         error: (function(_this) {
29           return function(e) {
30             _this.handlers.error(e);
31             return _this._closeOnError();
32           };
33         })(this),
34         message: (function(_this) {
35           return function(message) {
36             return _this.handlers.message(message);
37           };
38         })(this)
39       });
40       this._handshakeTimeout = new Timer((function(_this) {
41         return function() {
42           if (!_this._isSocketConnected()) {
43             return;
44           }
45           _this._disconnectionReason = 'handshake-timeout';
46           return _this.socket.close();
47         };
48       })(this));
49       this._reconnectTimer = new Timer((function(_this) {
50         return function() {
51           if (!_this._connectionDesired) {
52             return;
53           }
54           return _this.connect();
55         };
56       })(this));
57       this.connect();
58     }
59
60     Connector.prototype._isSocketConnected = function() {
61       return this.socket && this.socket.readyState === this.WebSocket.OPEN;
62     };
63
64     Connector.prototype.connect = function() {
65       this._connectionDesired = true;
66       if (this._isSocketConnected()) {
67         return;
68       }
69       this._reconnectTimer.stop();
70       this._disconnectionReason = 'cannot-connect';
71       this.protocolParser.reset();
72       this.handlers.connecting();
73       this.socket = new this.WebSocket(this._uri);
74       this.socket.onopen = (function(_this) {
75         return function(e) {
76           return _this._onopen(e);
77         };
78       })(this);
79       this.socket.onclose = (function(_this) {
80         return function(e) {
81           return _this._onclose(e);
82         };
83       })(this);
84       this.socket.onmessage = (function(_this) {
85         return function(e) {
86           return _this._onmessage(e);
87         };
88       })(this);
89       return this.socket.onerror = (function(_this) {
90         return function(e) {
91           return _this._onerror(e);
92         };
93       })(this);
94     };
95
96     Connector.prototype.disconnect = function() {
97       this._connectionDesired = false;
98       this._reconnectTimer.stop();
99       if (!this._isSocketConnected()) {
100         return;
101       }
102       this._disconnectionReason = 'manual';
103       return this.socket.close();
104     };
105
106     Connector.prototype._scheduleReconnection = function() {
107       if (!this._connectionDesired) {
108         return;
109       }
110       if (!this._reconnectTimer.running) {
111         this._reconnectTimer.start(this._nextDelay);
112         return this._nextDelay = Math.min(this.options.maxdelay, this._nextDelay * 2);
113       }
114     };
115
116     Connector.prototype.sendCommand = function(command) {
117       if (this.protocol == null) {
118         return;
119       }
120       return this._sendCommand(command);
121     };
122
123     Connector.prototype._sendCommand = function(command) {
124       return this.socket.send(JSON.stringify(command));
125     };
126
127     Connector.prototype._closeOnError = function() {
128       this._handshakeTimeout.stop();
129       this._disconnectionReason = 'error';
130       return this.socket.close();
131     };
132
133     Connector.prototype._onopen = function(e) {
134       var hello;
135       this.handlers.socketConnected();
136       this._disconnectionReason = 'handshake-failed';
137       hello = {
138         command: 'hello',
139         protocols: [PROTOCOL_6, PROTOCOL_7]
140       };
141       hello.ver = Version;
142       if (this.options.ext) {
143         hello.ext = this.options.ext;
144       }
145       if (this.options.extver) {
146         hello.extver = this.options.extver;
147       }
148       if (this.options.snipver) {
149         hello.snipver = this.options.snipver;
150       }
151       this._sendCommand(hello);
152       return this._handshakeTimeout.start(this.options.handshake_timeout);
153     };
154
155     Connector.prototype._onclose = function(e) {
156       this.protocol = 0;
157       this.handlers.disconnected(this._disconnectionReason, this._nextDelay);
158       return this._scheduleReconnection();
159     };
160
161     Connector.prototype._onerror = function(e) {};
162
163     Connector.prototype._onmessage = function(e) {
164       return this.protocolParser.process(e.data);
165     };
166
167     return Connector;
168
169   })();
170
171 }).call(this);