-
Notifications
You must be signed in to change notification settings - Fork 11
/
client-reconnect.js
68 lines (64 loc) · 2.16 KB
/
client-reconnect.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
"use strict";
var net = require('net');
var util = require('util');
var backoff = require('backoff');
var events = require('events');
var ClientConnection = require('./client-connection');
var copy = require('shallow-copy');
var ClientReconnect = module.exports = function (options,newConnection) {
this.socket = null;
this.connect = backoff.fibonacci({
initialDelay: 10,
randomisationFactor: 0.15,
maxDelay: 10000,
});
this.newConnection = newConnection;
this.options = options;
var self = this;
this.connect.on('ready', function () { self._readyToConnect() });
this.backoff = function () { self.connect.backoff() }
this._readyToConnect();
events.EventEmitter.call(this);
}
util.inherits(ClientReconnect, events.EventEmitter);
ClientReconnect.prototype.disconnect = function () {
this.connect.reset();
if (this.socket) {
this.socket.removeAllListeners('disconnect');
this.socket.removeAllListeners('error');
this.socket.disconnect();
this.socket = null;
this.emit('disconnect',this);
}
}
ClientReconnect.prototype._readyToConnect = function () {
var self = this;
var socket = this.newConnection(function(){ self._socketConnected(socket) });
socket.once('error', this.backoff);
}
ClientReconnect.prototype._socketConnected = function (socket) {
var options = copy(this.options);
options.socket = socket;
// handshake with server
var self = this;
var client = new ClientConnection(options,function() { self._connectionReady(client) });
client.once('error', this.backoff);
socket.removeListener('error', this.backoff);
}
ClientReconnect.prototype._connectionReady = function (client) {
client.removeListener('error', this.backoff);
// if that went well, we get to continue
this.connect.reset();
this.socket = client;
var self = this;
client.once('disconnect', function () {
self.socket = null;
self.backoff();
self.emit('disconnect',self);
});
client.once('error', function (error) {
self.emit('error',error,self);
});
client.setMaxListeners(0);
self.emit('connect',self);
}