javascript - NodeJS Nested Event listeners -


I do not get it, with the nested event listeners, the logic argument of the event emitter why do all the values ​​put in currents? Is that because it has to go through the upper level 'linking' listener? Is there variable information anywhere?

  var events = required ('event'); Var net = requirement ('net'); Var channel = new events Enter Emiter (); Var i = 0; Var Subscriptions; // If we have two connection channels. ('Join', function) {// Upper listener console log (subs); // - & gt; Output 0 When the first customer joins and for the second channel 1. 'broadcast', the function (subs2) {// Less Listener Console.log (subs); // Stream of All Connections: - & gt; 0 and 1 ??? console.log (subs2); // Output Last Connection - & gt; 1});} ); Var server = net.createServer (function (client) {subscriptions = i ++; // variable} channel.emit ('join', subscriptions); // To pass the same variable client. ('Data', function (data) {channel.emit ('broadcast', subscription); // same variable}};}); server.listen (7000);   

This TCP server creates you then telnet localhost 7000 ,

Please change the channel. ('Broadcast', ...) with channel.once ('broadcast', ...). So use 'one-time' subscription which will remove the 'broadcast' listener once handled.

We had a 'broadcast' subscription for each 'included' subscription. Let's say that after joining 3 there will be three memberships for the 'broadcast' event. So when the emitter emits with 'broadcast', then all three memberships are called. The value of the sub is the previous value and only sub-2 has been updated.

The modified code will look like this. I'd like to put some extra console logs to better understand better.

  var events = need ('events'); Var net = requirement ('net'); Var channel = new events Enter Emiter (); Var i = 0; Var Subscriptions; // If we have two connection channels. ("Included", function (subs) {// Upper listener console log ("include: subs:" + subs); // -> output 0 when the first client was added and 1 for another channel. ('Broadcast', subs2) {// less listeners console log ('came in the broadcast'); console.log ("broadcast: subs:" + subs); // stream connection of all: - & gt; ; 0 and 1 ??? console.log ("Broadcast: subs2:" + subs2); // Outputs Last Connection - & gt; 1});}); Var server = net.createServer (function (client) {subscriptions = i ++; // variable} channel.emit ('join', subscriptions); // To pass the same variable client. ('Data', function (data) {console.log ('received data:' + data); channel.mate ('broadcast', subscriptions); // pass the same variable}};}); Server.listen (7000);    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -