better handling of web socket listeners

This commit is contained in:
Dave Conway-Jones 2016-06-02 12:35:12 +01:00
parent 47571bcb73
commit 4647189130
2 changed files with 14 additions and 10 deletions

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-contrib-web-worldmap",
"version" : "1.0.8",
"version" : "1.0.9",
"description" : "A Node-RED node to provide a web page of a world map for plotting things on.",
"dependencies" : {
"express": "4.*",

View File

@ -25,19 +25,21 @@ module.exports = function(RED) {
//node.log("Serving map from "+__dirname+" as "+RED.settings.httpNodeRoot.slice(0,-1)+"/worldmap");
RED.httpNode.use("/worldmap", express.static(__dirname + '/worldmap'));
var callback = function(socket) {
node.status({fill:"green",shape:"dot",text:"connected"});
node.status({fill:"green",shape:"dot",text:"connected "+Object.keys(io.sockets.connected).length});
node.on('input', function(msg) {
socket.emit("worldmapdata",msg.payload);
});
socket.on('disconnect', function() {
node.status({fill:"red",shape:"ring",text:"disconnected"});
node.status({fill:"green",shape:"ring",text:"connected "+Object.keys(io.sockets.connected).length});
});
node.on("close", function() {
node.status({});
socket.disconnect();
io.sockets.removeListener('connection', callback);
});
}
node.on("close", function() {
node.status({});
io.sockets.removeListener('connection', callback);
});
io.on('connection', callback );
}
RED.nodes.registerType("worldmap",WorldMap);
@ -46,20 +48,22 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
var node = this;
var callback = function(socket) {
node.status({fill:"green",shape:"dot",text:"connected"});
node.status({fill:"green",shape:"dot",text:"connected "+Object.keys(io.sockets.connected).length});
socket.on('worldmap', function(data) {
node.send({payload:data, topic:"worldmap"});
});
socket.on('disconnect', function() {
node.status({fill:"red",shape:"ring",text:"disconnected"});
node.send({payload:{action:"disconnect"}, topic:"worldmap"});
node.status({fill:"green",shape:"ring",text:"connected "+Object.keys(io.sockets.connected).length});
node.send({payload:{action:"disconnect", clients:Object.keys(io.sockets.connected).length}, topic:"worldmap"});
});
node.on("close", function() {
node.status({});
socket.disconnect();
io.sockets.removeListener('connection', callback);
});
}
node.on("close", function() {
node.status({});
io.sockets.removeListener('connection', callback);
});
io.on('connection', callback);
}
RED.nodes.registerType("worldmap in",WorldMapIn);