Add _session and event to WS/TCP Status messages

This commit is contained in:
Nick O'Leary 2019-03-04 11:09:18 +00:00
parent 8fb6bc059e
commit 19a103d3a0
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 66 additions and 18 deletions

View File

@ -72,19 +72,19 @@ module.exports = function(RED) {
var id = (1+Math.random()*4294967295).toString(16);
if (node.isServer) {
node._clients[id] = socket;
node.emit('opened',Object.keys(node._clients).length);
node.emit('opened',{count:Object.keys(node._clients).length,id:id});
}
socket.on('open',function() {
if (!node.isServer) {
node.emit('opened','');
node.emit('opened',{count:'',id:id});
}
});
socket.on('close',function() {
if (node.isServer) {
delete node._clients[id];
node.emit('closed',Object.keys(node._clients).length);
node.emit('closed',{count:Object.keys(node._clients).length,id:id});
} else {
node.emit('closed');
node.emit('closed',{count:'',id:id});
}
if (!node.closing && !node.isServer) {
clearTimeout(node.tout);
@ -95,7 +95,7 @@ module.exports = function(RED) {
node.handleEvent(id,socket,'message',data,flags);
});
socket.on('error', function(err) {
node.emit('erro');
node.emit('erro',{err:err,id:id});
if (!node.closing && !node.isServer) {
clearTimeout(node.tout);
node.tout = setTimeout(function() { startconn(); }, 3000); // try to reconnect every 3 secs... bit fast ?
@ -230,14 +230,30 @@ module.exports = function(RED) {
if (this.serverConfig) {
this.serverConfig.registerInputNode(this);
// TODO: nls
this.serverConfig.on('opened', function(n) { node.status({fill:"green",shape:"dot",text:RED._("websocket.status.connected",{count:n})}); });
this.serverConfig.on('erro', function() { node.status({fill:"red",shape:"ring",text:"common.status.error"}); });
this.serverConfig.on('closed', function(n) {
if (n > 0) {
node.status({fill:"green",shape:"dot",text:RED._("websocket.status.connected",{count:n})});
this.serverConfig.on('opened', function(event) {
node.status({
fill:"green",shape:"dot",text:RED._("websocket.status.connected",{count:event.count}),
event:"connect",
_session: {type:"websocket",id:event.id}
});
});
this.serverConfig.on('erro', function(event) {
node.status({
fill:"red",shape:"ring",text:"common.status.error",
event:"error",
_session: {type:"websocket",id:event.id}
});
});
this.serverConfig.on('closed', function(event) {
var status;
if (event.count > 0) {
status = {fill:"green",shape:"dot",text:RED._("websocket.status.connected",{count:event.count})};
} else {
node.status({fill:"red",shape:"ring",text:"common.status.disconnected"});
status = {fill:"red",shape:"ring",text:"common.status.disconnected"};
}
status.event = "disconnect";
status._session = {type:"websocket",id:event.id}
node.status(status);
});
} else {
this.error(RED._("websocket.errors.missing-conf"));
@ -261,11 +277,30 @@ module.exports = function(RED) {
}
else {
// TODO: nls
this.serverConfig.on('opened', function(n) { node.status({fill:"green",shape:"dot",text:RED._("websocket.status.connected",{count:n})}); });
this.serverConfig.on('erro', function() { node.status({fill:"red",shape:"ring",text:"common.status.error"}); });
this.serverConfig.on('closed', function(n) {
if (n > 0) { node.status({fill:"green",shape:"dot",text:RED._("websocket.status.connected",{count:n})}); }
else { node.status({fill:"red",shape:"ring",text:"common.status.disconnected"}); }
this.serverConfig.on('opened', function(event) {
node.status({
fill:"green",shape:"dot",text:RED._("websocket.status.connected",{count:event.count}),
event:"connect",
_session: {type:"websocket",id:event.id}
});
});
this.serverConfig.on('erro', function(event) {
node.status({
fill:"red",shape:"ring",text:"common.status.error",
event:"error",
_session: {type:"websocket",id:event.id}
})
});
this.serverConfig.on('closed', function(event) {
var status;
if (event.count > 0) {
status = {fill:"green",shape:"dot",text:RED._("websocket.status.connected",{count:event.count})};
} else {
status = {fill:"red",shape:"ring",text:"common.status.disconnected"};
}
status.event = "disconnect";
status._session = {type:"websocket",id:event.id}
node.status(status);
});
}
this.on("input", function(msg) {

View File

@ -158,7 +158,13 @@ module.exports = function(RED) {
var fromp;
connectionPool[id] = socket;
count++;
node.status({text:RED._("tcpin.status.connections",{count:count})});
node.status({
text:RED._("tcpin.status.connections",{count:count}),
event:"connect",
ip:socket.remoteAddress,
port:socket.remotePort,
_session: {type:"tcp",id:id}
});
var buffer = (node.datatype == 'buffer') ? Buffer.alloc(0) : "";
socket.on('data', function (data) {
@ -209,7 +215,14 @@ module.exports = function(RED) {
socket.on('close', function() {
delete connectionPool[id];
count--;
node.status({text:RED._("tcpin.status.connections",{count:count})});
node.status({
text:RED._("tcpin.status.connections",{count:count}),
event:"disconnect",
ip:socket.remoteAddress,
port:socket.remotePort,
_session: {type:"tcp",id:id}
});
});
socket.on('error',function(err) {
node.log(err);