tcpclient - Transmit data from client to server in node.js -


i'm building simple chat room using tcp connection , node.js. i'm expecting text transmitted after "enter", what's happened instead each character sent after pressing it. here code...

var server = net.createserver(function(conn){     console.log('\033[92m  new connection! \033[39m');     conn.write('> welcome \033[92mnode-chat\033[39m! \n'     + '> ' + count + ' other people connected @ time.'     + '\n > please write name , press enter: '   );    count ++;   conn.setencoding('utf8');   conn.on('data', function(data){         console.log(data);   });    conn.on('close', function(){         count --;     }); }); 

it sounds telnet send each character own tcp request.
recommend different approach in listen on sockets created on each connection. way in future able manage each socket own , not central location may become tedious:

var server = net.createconnection(...   ... }); server.on('connection', function(socket, connection){   //i'm adding buffer socket although might not need (try remove , see happened)   socket.buf = '';   var self = this; //use if 'this' not work. (explanation why confuse here if there need explain)   //create listener each socket   socket.on('data', function(data){     //since telnet send each character in it's own need monitor 'enter' character     if( (data=='\\r\\n') || (data=='\\n') ){       console.log(this.buf);//if 'this' doesn't work try use 'self'       this.buf = '';     }     else //no 'enter' character concat data buffer.       this.buf += data;   });   socket.on('end', function(){     //socket closing (not closed yet) let's print have.     if(this.buf && (this.buf.length > 0) )       console.log(this.buf);   }); }); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -