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