innogy-smarthome-lib
Advanced tools
Comparing version
const inherits = require('util').inherits; | ||
const EventEmitter = require('events').EventEmitter; | ||
const WebSocket = require('ws'); | ||
const WebSocketClient = require('./../websocketclient'); | ||
@@ -164,3 +163,8 @@ const Promise = require('bluebird'); | ||
if (this._wsClient) { | ||
this._wsClient.close(); | ||
try { | ||
this._wsClient.close(); | ||
} catch (e) { | ||
// Ignore | ||
} | ||
this._wsClient = null; | ||
@@ -452,4 +456,8 @@ } | ||
if (that._wsClient) { | ||
that._wsClient.close(); | ||
that._wsClient = null; | ||
try { | ||
that._wsClient.close(); | ||
that._wsClient = null; | ||
} catch (e) { | ||
// Ignore | ||
} | ||
} | ||
@@ -460,2 +468,3 @@ | ||
ws.on('message', function (data) { | ||
var eventArray = JSON.parse(data); | ||
@@ -462,0 +471,0 @@ that.emit("debug", {type: "realtime_update_received", data: eventArray}); |
@@ -73,3 +73,4 @@ const inherits = require('util').inherits; | ||
Capability.prototype.parseConfig = function (config, stateChangedCallback) { | ||
Capability.super_.prototype.parseConfig.call(this, config); | ||
if (!stateChangedCallback) | ||
Capability.super_.prototype.parseConfig.call(this, config); | ||
@@ -76,0 +77,0 @@ var that = this; |
@@ -13,2 +13,3 @@ const inherits = require('util').inherits; | ||
this._reconnecting = false; | ||
this._reconnectInterval = null; | ||
@@ -21,5 +22,2 @@ this.open(url); | ||
WebSocketClient.prototype.getReadyState = function() { | ||
return this.instance.readyState; | ||
}; | ||
@@ -42,7 +40,13 @@ WebSocketClient.prototype.open = function (url) { | ||
this._closed = false; | ||
this._reconnectInterval = null; | ||
this.instance = new WebSocket(url); | ||
this.instance.on('open', function () { | ||
this._reconnecting = false; | ||
that._reconnecting = false; | ||
if (that._reconnectInterval) { | ||
clearInterval(that._reconnectInterval); | ||
that._reconnectInterval = null; | ||
} | ||
that.onopen(); | ||
@@ -73,4 +77,6 @@ }); | ||
this._closed = true; | ||
this.instance.close(); | ||
if (this.instance.readyState == WebSocket.OPEN) | ||
this.instance.close(); | ||
if (this._reconnectInterval) { | ||
@@ -80,2 +86,4 @@ clearInterval(this._reconnectInterval); | ||
} | ||
this.onclose(); | ||
}; | ||
@@ -99,10 +107,21 @@ | ||
if (this._reconnectInterval) | ||
if (this._reconnectInterval) { | ||
clearInterval(this._reconnectInterval); | ||
this._reconnectInterval = null; | ||
} | ||
if (!this._closed) { | ||
this._reconnectInterval = setInterval(function () { | ||
var interval = setInterval(function () { | ||
that.emit("reconnect"); | ||
that.open(that.url); | ||
if (that.instance.readyState !== WebSocket.OPEN) | ||
that.open(that.url); | ||
else { | ||
that._reconnecting = false; | ||
clearInterval(that._reconnectInterval); | ||
that._reconnectInterval = null; | ||
} | ||
}, this.autoReconnectInterval); | ||
this._reconnectInterval = interval; | ||
} | ||
@@ -112,5 +131,2 @@ }; | ||
WebSocketClient.prototype.onopen = function (e) { | ||
if (this._reconnectInterval) | ||
clearInterval(this._reconnectInterval); | ||
this.emit("open", e); | ||
@@ -117,0 +133,0 @@ }; |
{ | ||
"name": "innogy-smarthome-lib", | ||
"version": "0.2.14", | ||
"version": "0.2.15", | ||
"description": "JavaScript library for the Innogy SmartHome system", | ||
@@ -5,0 +5,0 @@ "main": "./lib/smarthome", |
64291
0.91%1417
1.29%