javascript - Nodejs - Simple Echo Server not working -


I do not understand why I can not write in the socket. I do not know whether my event is made properly or not, I am trying to learn Nodes and I'm new to JavaScript.

  var events = need ('events'); Var Net = Requirement ('Net'); Var channel = new events Enter Emiter (); Channel.on ('included', function (socket) {this.on ('broadcast', function (data) {socket.write (data);})}}); Var server = net.createServer (function (socket) {socket.on ('connect', function) {channel.emit ('join', socket);}); Socket.on ('data', function (data) {data = data.toString (); channel.emit ('broadcast', data);});}); Server.listen (8888);    

The event connect for a socket is only emitted My understanding, when you connect as a client; You're doing a server here, however, when callback is inside the net.createServer , you're already connected to the socket, even if the connect event is emitted Even if you have gone, you will subscribe with it socket.one ('connect', ... ... is too late.)

In return, because connect < / Code> Callback is not called, you are never emitted on joining event and on the event channel The broadcast handle event handler is never registered, and the socket. Writing statement is never called. As mentioned, some console.log statement will help you to figure out which code is actually executed and reducing this issue.

You can just fix it by removing the socket (but 'connect', ...) and the external function is doing it correctly, i.e.,

  var server = net.createServer (function (socket) {console .log ("connect!"); Channel.emit ('join', socket); socket.on ('data', function Data) {...    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -