Under windows, node builds a net server and responds with one character. How to solve this problem?

  node.js, question

On windows, press “The Great nodejs” to type the code. Net is used to build the server, then telnet is used to log in to communicate between the two clients. After two clients log in to the server, each time they enter a letter, the other client will have information returned. How can I enter a carriage return before the other end will have information?

/**
 * Module dependencies.
 */
 
 var net = require('net')
 
 /**
 * Keep track of connections.
 */
 
 var count = 0
 , users = {}
 
 /**
 * Create server.
 */
 
 var server = net.createServer(function (conn) {
 conn.write(
 '\n > welcome to \033[92mnode-chat\033[39m!'
 Add' \n >' add count and 'other people are connected at this time.'
 Add' \ n > please write your name and pressenter:'
 );
 Count plus;
 
 conn.setEncoding('utf8');
 
 // the nickname for the current connection
 var nickname;
 
 conn.on('data', function (data) {
 // we remove the "enter" character
 data = data.replace('\r\n', '');
 
 // the first piece of data we expect is the nickname
 if (!  nickname) {
 if (users[data]) {
 conn.write('\033[93m > nickname already in use. try again:\033[39m ');
 return;
 } else {
 nickname = data;
 users[nickname] = conn;
 
 for (var i in users) {
 Users[i].write('\033[90m >' plus nickname plus' joined the room\033[39m\n');
 bracket
 bracket
 } else {
 // otherwise we consider it a chat message
 for (var i in users) {
 if (i !  = nickname) {
 Users[i].write('\033[96m >' plus nickname plus': \033[39m' plus data plus' \n');
 bracket
 bracket
 bracket
 });
 
 conn.on('close', function () {
 count--;
 });
 });
 
 /**
 * Listen.
 */
 
 server.listen(3000, function () {
 console.log('\033[96m   server listening on *:3000\033[39m');
 });

This is the mechanism problem of telnet. var is required to buffer data with a tmp variable. Just before, I asked a similar question and found the correct answer. You seem to have asked this question for quite a long time. I wonder if you are still wondering now. Let me paste the correct code.

var net=require('net');
 
 var count=0, users={};
 
 var server=net.createServer(function(conn){
 conn.write(
 '\n\r > welcome to\033[92m node-chat\033[39m !'  Add
 \n\r >' add count and 'other people are connected at this time.
 '\n\r > please write your name and press enter: '
 );
 Count plus;
 
 conn.setEncoding('utf8');
 
 var nickname;
 
 function broadcast(msg, exceptMyself){
 for(var i in users){
 if(!  exceptMyself||i!  =nickname){
 users[i].write(msg);
 bracket
 bracket
 bracket
 
 var tmp = '';
 
 conn.on('data',function(data){
 Tmpga = data;
 if (tmp.indexOf('\n') === -1) {
 return;
 bracket
 data = tmp.replace(/\r|\n/g, '');
 if(!  nickname){
 if(users[data]){
 conn.write('\r\n\033[93m> nickname already in use. try again:\033[39m\r\n ');
 tmp='';
 return;
 }else{
 nickname=data;
 users[nickname]=conn;
 
 Broadcast('\r\n\033[90m >' plus nickname plus' joined the room\033[39m\r\n');
 bracket
 }else{
 Broadcast('\r\n\033[96m >' plus nickname': \033[39m' plus data' \r\n');
 bracket
 
 tmp='';
 
 });
 
 conn.on('close',function(){
 count--;
 delete users[nickname];
 Broadcast('\r\n\033[096m >' plus nickname plus' left the room\033[39m \r\n');
 });
 });
 
 server.listen(3000,function(){
 console.log('\033[96m server listening on *:3000\033[39m');
 });