Reconnect on lost connection

This commit is contained in:
Foxi352 2016-07-03 19:04:42 +02:00
parent 40afa67a3a
commit b84dee1a86

View File

@ -1,6 +1,8 @@
var http = require("http"); var http = require("http");
var WebSocketClient = require('websocket').client var WebSocketClient = require('websocket').client
var colorOn = "\x1b[30;47m";
var colorOff = "\x1b[0m";
function SmartHomeNGConnection(platform, log, host, port) { function SmartHomeNGConnection(platform, log, host, port) {
this.log = log; this.log = log;
@ -8,6 +10,7 @@ function SmartHomeNGConnection(platform, log, host, port) {
this.connected = false; this.connected = false;
this.updateCallback = undefined; this.updateCallback = undefined;
this.tomonitor = []; this.tomonitor = [];
this.retryTimer = 10;
this.shng_host = host; this.shng_host = host;
this.shng_port = port; this.shng_port = port;
@ -15,30 +18,38 @@ function SmartHomeNGConnection(platform, log, host, port) {
} }
SmartHomeNGConnection.prototype.init = function () { SmartHomeNGConnection.prototype.init = function () {
this.connect();
}
SmartHomeNGConnection.prototype.connect = function(host, ip) {
var that = this var that = this
this.log("Connecting to SmartHomeNG @ " + this.shng_host);
this.shng_ws = new WebSocketClient(); this.shng_ws = new WebSocketClient();
this.shng_ws.connect('ws://' + this.shng_host + ':' + this.shng_port + '/');
this.shng_ws.on('connect', function(connection) { this.shng_ws.on('connect', function(connection) {
that.log('connected to server!'); that.log('[SmartHomeNGConnection] connected to server!');
that.connected = true; that.connected = true;
that.connection = connection; that.connection = connection;
that.startMonitoring(); that.startMonitoring();
connection.on('message', function(message) { that.receive(message); }); connection.on('message', function(message) { that.receive(message); });
connection.on('error', function(error) { that.log('WebSocket error: ' + error.toString()) }); connection.on('error', function() {
connection.on('close', function() { that.log(colorOn + '[SmartHomeNGConnection] WebSocket error: ' + error.toString() + colorOff)
that.log('Connection to smarthome lost'); });
that.connected = false; connection.on('close', function(code, description) {
that.log(colorOn + '[SmartHomeNGConnection] Connection to smarthome lost, retrying in ' + that.retryTimer + ' seconds. ' + description + colorOff);
that.connected = false;
setTimeout(that.connect.bind(that), that.retryTimer * 1000);
}); });
}); });
this.shng_ws.on('connectFailed', function(errorDescription) {
that.connected = false;
that.log(colorOn + '[SmartHomeNGConnection] Connection error, retrying in ' + that.retryTimer + ' seconds. ' + errorDescription + colorOff);
setTimeout(that.connect.bind(that), that.retryTimer * 1000);
});
this.connect();
}
SmartHomeNGConnection.prototype.connect = function(host, ip) {
this.log("[SmartHomeNGConnection] Connecting to SmartHomeNG @ " + this.shng_host);
this.shng_ws.connect('ws://' + this.shng_host + ':' + this.shng_port + '/');
} }