Added a socket angel to force reconnection to the MQTT WS when it disconnects. If no messages are received from it for 60 seconds it forces a disconnect.

This commit is contained in:
Martin Donnelly 2016-05-18 11:11:31 +01:00
parent f0c04db85d
commit 7c5abc894b
2 changed files with 31 additions and 4 deletions

1
app.js
View File

@ -89,6 +89,7 @@ busEmitter.on('projectorOff', mqttConnect.projectorOff);
busEmitter.on('connectWS', mqttConnect.connectWS);
busEmitter.on('restartMQTTSocket', mqttConnect.restartMQTTSocket);
mqttConnect.setupPing();
cal.startController(busEmitter);

View File

@ -29,9 +29,11 @@ var live = true;
logger.warn('!!! Live? ', live);
module.exports = {
restartTimer : 0,
pingTimer: 0,
statuses: {},
sockets: null,
wsClient: null,
watches: {},
client: null,
projector: live ? 'Projector' : 'ProjectorISP15',
@ -212,7 +214,7 @@ module.exports = {
},
sendRefresh: function() {
logger.debug('+ Send refresh', this.statuses);
// logger.debug('+ Send refresh', this.statuses);
for (var item in this.statuses) {
if (this.statuses.hasOwnProperty(item)) {
@ -220,7 +222,21 @@ module.exports = {
this.emitter.emit('sendSocket', this.statuses[item]);
}
}
logger.debug('+ Send refresh');
// logger.debug('+ Send refresh');
},
preRestartConnection: function() {
logger.debug('Restart connection...');
//this.emitter.emit('restartMQTTSocket', this);
logger.debug(this);
this.restartMQTTSocket();
},
startMQTTSocket:function() {
logger.warn('Restarting socket?');
this.connectWS();
},
restartMQTTSocket: function() {
this.wsClient.disconnect();
// setTimeout(this.startMQTTSocket.bind(this), 15000);
},
connectWS: function(connectCB) {
@ -232,12 +248,19 @@ module.exports = {
var api_key = 'a-qz0da4-dfwwdkmkzr';
var auth_token = '9txJEf3Cjy7hkSOvkv';
var wsClient = new Messaging.Client(hostname, 8883, clientId);
this.wsClient =new Messaging.Client(hostname, 8883, clientId);
//var wsClient = new Messaging.Client(hostname, 8883, clientId);
var wsClient = this.wsClient;
var clientStatus = {connected: false, subscribed: false, deviceConnected: false};
var sensorData = {};
this.restartTimer = setTimeout(this.preRestartConnection.bind(this), 60000);
wsClient.onMessageArrived = function(msg) {
logger.info("Message from :" + msg.destinationName);
//logger.info("Message from :" + msg.destinationName);
clearTimeout(self.restartTimer);
self.restartTimer = setTimeout(self.preRestartConnection.bind(self), 60000);
clientStatus.deviceConnected = true;
sensorData = JSON.parse(msg.payloadString);
// Logger.debug(sensorData);
@ -253,6 +276,8 @@ module.exports = {
wsClient.onConnectionLost = function(e) {
logger.error('+ wsClient.onConnectionLost');
logger.error(e);
logger.warn('Going to force a restart of the Socket.. Hold on.');
setTimeout(self.startMQTTSocket.bind(self), 15000);
//self.emitter.emit('sendSocket',{id: 'deviceLost',data: e});
//var wsReconnectTimer = setTimeout(function() {logger.debug('TRYING TO RECONNECT TO MQTT');self.emitter.emit('connectWS');}.bind(this),30000);
logger.error('- wsClient.onConnectionLost');
@ -323,6 +348,7 @@ module.exports = {
}
};