Hier wie versprochen mein Code für NodeJS, der eigentlich soweit schon gut klappt:
Ein automatischer re-connect ist mit eingebaut. Bitte acht geben, dass in den CMD Einstellungen der Quick Edit Mode aus ist, sonst schläft die CMD ein:
Code
const WebSocket = require('ws');
const fs = require('fs');
const os = require('os');
function LogOut(log_data){
log_data = new Date().toLocaleString()+" "+log_data
console.log(log_data);
fs.appendFile('C:\pfad-wo-das-skript-liegt\log.txt', log_data + os.EOL, function(err, result) {
if(err) console.log('error', err);
});
}
function WebSocketClient(){
this.number = 0; // Message number
this.autoReconnectInterval = 10*1000; // ms
this.timeout = 60*1000; // ms
}
WebSocketClient.prototype.open = function(e){
this.instance = new WebSocket('wss://*********************:Port', {
headers: {
"master_token": "***************************************",
"sub_token": "***************************************",
},
ca: [
fs.readFileSync('<<dateiname>>.pem')
],
});
this.instance.on('open',()=>{
LogOut("WebSocket: connected");
var that = this;
clearTimeout(that.myTimer);
that.myTimer = setTimeout(function(){
LogOut("WebSocket: Timeout of Server Ping");
that.reconnect(e);
}, this.timeout);
this.onopen();
});
this.instance.on('message',(data,flags)=>{
LogOut("WebSocket: message received");
this.number ++;
this.onmessage(data,flags,this.number);
});
this.instance.on('close',(e)=>{
LogOut("WebSocket: closed" + e.code);
switch (e.code){
case 1000: // CLOSE_NORMAL
break;
default: // Abnormal closure
break;
}
this.reconnect(e);
this.onclose(e);
});
this.instance.on('error',(e)=>{
LogOut("WebSocket: error:" + e.code);;
switch (e.code){
case 'ECONNREFUSED':
this.reconnect(e);
break;
default:
this.onerror(e);
break;
}
});
this.instance.on('ping',(e)=>{
LogOut("WebSocket: ping received");
var that = this;
clearTimeout(that.myTimer);
that.myTimer = setTimeout(function(){
LogOut("WebSocket: Timeout of Server Ping");
that.reconnect(e);
}, this.timeout);
});
this.instance.on('handshake',(e)=>{
LogOut("WebSocket: handshake received");
});
this.instance.on('rawdata',(e)=>{
LogOut("WebSocket: rawdata received");
});
}
WebSocketClient.prototype.reconnect = function(e){
LogOut(`WebSocketClient: retry in ${this.autoReconnectInterval} ms `+e);
this.instance.removeAllListeners();
var that = this;
setTimeout(function(){
LogOut("WebSocketClient: reconnecting...");
that.open();
}, this.autoReconnectInterval);
}
WebSocketClient.prototype.close = function(e){
LogOut("WebSocket: closing Websocket");
this.instance.removeAllListeners();
}
WebSocketClient.prototype.onopen = function(e){
LogOut("WebSocketClient: open" + arguments);
}
WebSocketClient.prototype.onmessage = function(data,flags,number){
LogOut("WebSocketClient: message" + arguments);
}
WebSocketClient.prototype.onerror = function(e){
LogOut("WebSocketClient: error" + arguments);
}
WebSocketClient.prototype.onclose = function(e){
LogOut("WebSocketClient: closed" + arguments);
}
var wsc = new WebSocketClient();
wsc.open();
wsc.onopen = function(e){
LogOut("WebSocketClient connected:",e);
}
wsc.onmessage = function(data,flags,number){
LogOut(`WebSocketClient message #${number}: ` + data);
jdata = JSON.parse(data);
switch (jdata.type) {
case "error":
switch (jdata.statusCode) {
case "auth_failed":
LogOut('Katsys error: Authentication Error');
wsc.close();
break;
case "already_connected":
LogOut('Katsys error: Already connected');
wsc.close();
break;
}
case "data":
switch (jdata.statusCode) {
case "radio_status":
//Status
LogOut('Katsys status: ' + jdata.msg);
}
break;
case "alarm_data":
LogOut("Katsys message: " + jdata.msg);
var alarmtext="";
for (var key in jdata["data"]["textElements"]) {
alarmtext = alarmtext + key + ":" + jdata["data"]["textElements"][key] + os.EOL
}
fs.appendFile('C:/Users/User/Einsatz_Monitor/Text_Input/alarm.txt', alarmtext, function(err, result) {
if(err) console.log('error', err);
});
}
}
}
Alles anzeigen