2 var Connector, PROTOCOL_6, PROTOCOL_7, Parser, Version, _ref;
4 _ref = require('./protocol'), Parser = _ref.Parser, PROTOCOL_6 = _ref.PROTOCOL_6, PROTOCOL_7 = _ref.PROTOCOL_7;
8 exports.Connector = Connector = (function() {
9 function Connector(options, WebSocket, Timer, handlers) {
10 this.options = options;
11 this.WebSocket = WebSocket;
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;
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);
28 error: (function(_this) {
30 _this.handlers.error(e);
31 return _this._closeOnError();
34 message: (function(_this) {
35 return function(message) {
36 return _this.handlers.message(message);
40 this._handshakeTimeout = new Timer((function(_this) {
42 if (!_this._isSocketConnected()) {
45 _this._disconnectionReason = 'handshake-timeout';
46 return _this.socket.close();
49 this._reconnectTimer = new Timer((function(_this) {
51 if (!_this._connectionDesired) {
54 return _this.connect();
60 Connector.prototype._isSocketConnected = function() {
61 return this.socket && this.socket.readyState === this.WebSocket.OPEN;
64 Connector.prototype.connect = function() {
65 this._connectionDesired = true;
66 if (this._isSocketConnected()) {
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) {
76 return _this._onopen(e);
79 this.socket.onclose = (function(_this) {
81 return _this._onclose(e);
84 this.socket.onmessage = (function(_this) {
86 return _this._onmessage(e);
89 return this.socket.onerror = (function(_this) {
91 return _this._onerror(e);
96 Connector.prototype.disconnect = function() {
97 this._connectionDesired = false;
98 this._reconnectTimer.stop();
99 if (!this._isSocketConnected()) {
102 this._disconnectionReason = 'manual';
103 return this.socket.close();
106 Connector.prototype._scheduleReconnection = function() {
107 if (!this._connectionDesired) {
110 if (!this._reconnectTimer.running) {
111 this._reconnectTimer.start(this._nextDelay);
112 return this._nextDelay = Math.min(this.options.maxdelay, this._nextDelay * 2);
116 Connector.prototype.sendCommand = function(command) {
117 if (this.protocol == null) {
120 return this._sendCommand(command);
123 Connector.prototype._sendCommand = function(command) {
124 return this.socket.send(JSON.stringify(command));
127 Connector.prototype._closeOnError = function() {
128 this._handshakeTimeout.stop();
129 this._disconnectionReason = 'error';
130 return this.socket.close();
133 Connector.prototype._onopen = function(e) {
135 this.handlers.socketConnected();
136 this._disconnectionReason = 'handshake-failed';
139 protocols: [PROTOCOL_6, PROTOCOL_7]
142 if (this.options.ext) {
143 hello.ext = this.options.ext;
145 if (this.options.extver) {
146 hello.extver = this.options.extver;
148 if (this.options.snipver) {
149 hello.snipver = this.options.snipver;
151 this._sendCommand(hello);
152 return this._handshakeTimeout.start(this.options.handshake_timeout);
155 Connector.prototype._onclose = function(e) {
157 this.handlers.disconnected(this._disconnectionReason, this._nextDelay);
158 return this._scheduleReconnection();
161 Connector.prototype._onerror = function(e) {};
163 Connector.prototype._onmessage = function(e) {
164 return this.protocolParser.process(e.data);