UNPKG

crypto-nodes

Version:

97 lines (57 loc) 2.36 kB
module.exports = function(RED) { function socketChannelManager(config) { RED.nodes.createNode(this,config); var count = 0; var node = this; node.status({fill:"red",shape:"dot", text:"waiting for subscribers"}); node.on('input', function(msg) { msg.socket.on('subsribe', function(channel) { msg.socket.join(channel); count++; node.status({fill:"green",shape:"dot", text: msg.socket.handshake.address + ' joined ' + channel + ' (' + count + ')' }); }); msg.socket.on('unsubsribe', function(channel) { msg.socket.leave(channel); count++; node.status({fill:"red",shape:"dot", text: msg.socket.handshake.address + ' left ' + channel + ' (' + count + ')' }); }); }); } RED.nodes.registerType("socketChannelManager",socketChannelManager); function socketChannelBroadcaster(config) { RED.nodes.createNode(this,config); var count = 0; var node = this; node.status({fill:"red",shape:"dot", text:"nothing sent yet"}); node.on('input', function(msg) { count++; delete msg.PROVIDER; //global.app.io. var roomsAdapter = global.app.io.sockets.adapter.rooms; var roomName = msg.EXCHANGE + '/' + msg.FROMSYMBOL + '/' + msg.TOSYMBOL; if(roomsAdapter[roomName]) { var clients = roomsAdapter[roomName].sockets; //to get the number of clients var numClients = (typeof clients !== 'undefined') ? Object.keys(clients).length : 0; /* console.log('A'); console.log(global.app.io.sockets.adapter.rooms); console.log('B'); console.log(msg); console.log('C'); */ //console.log(clients); //console.log(numClients); var msgJson = JSON.stringify(msg); for (var clientId in clients ) { //this is the socket of each client in the room. var clientSocket = global.app.io.sockets.connected[clientId]; //you can do whatever you need with this clientSocket.emit('data', msgJson ); } node.status({fill:"green",shape:"dot", text: 'Processed ' + count + ' data ticks, last broadcasted to ' + numClients + ' clients' }); } }); } RED.nodes.registerType("socketChannelBroadcaster",socketChannelBroadcaster); }