-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddpconnector.js
39 lines (32 loc) · 994 Bytes
/
ddpconnector.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
// DDP Connector
DDPConnector = function (url, options = {}) {
this.url = url;
this.ddpOptions = options.ddpOptions || {};
this.timeout = options.timeout || 5 * 1000;
this.connection = DDP.connect(this.url, this.ddpOptions);
this.connection.disconnect();
this.startConnecting();
};
DDPConnector.prototype.getConnection = function () {
return this.connection;
};
DDPConnector.prototype.getUrl = function () {
return this.url;
};
DDPConnector.prototype.startConnecting = function () {
var self = this;
var connected = false;
setConnectionIfNeeded();
this.timeoutHandler = Meteor.setInterval(setConnectionIfNeeded, self.timeout);
function setConnectionIfNeeded () {
var status = self.connection.status();
var isOffline = connected && status.status == "offline";
if (!status.connected && !isOffline) {
self.connection.reconnect({ url: self.url });
connected = true;
}
}
};
DDPConnector.prototype.stop = function () {
Meteor.clearTimeout(this.timeoutHandler);
};