在服务器端建立后,客户端连接后需输入用户名,但是只输入一个字符就自动传到服务器端了,还没有按回车。
另外打开两个客户端后,在其中一个客户端输入后,两个客户端都会出现两行输入,三个客户端就会出现三行输入。。。依次类推。。。。
代码如下:
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 !'+
'\n\r > '+count+' other people are connected at this time.'+
'\n\r > please write your name and press enter: '
);
count++;
conn.setEncoding('utf8');
var nickname;
function broadcast(msg, exceptMyself){
for(var i in users){
if(!exceptMyself||i!=nickname){
users[i].write(msg);
}
}
}
conn.on('data',function(data){
data=data.replace('\r\n','');
if(!nickname){
if(users[data]){
conn.write('\r\n\033[93m> nickname already in use. try again:\033[39m\r\n ');
return;
}else{
nickname=data;
users[nickname]=conn;
broadcast('\r\n\033[90m > '+nickname+' joined the room\033[39m\r\n');
}
}else{
for(var i in users){
broadcast('\r\n\033[96m> '+nickname+':\033[39m '+data+'\r\n');
}
}
process.stdout.write(data);
});
conn.on('close',function(){
count--;
delete users[nickname];
broadcast('\r\n\033[096m> '+nickname+' left the room\033[39m \r\n');
});
});
server.listen(3000,function(){
console.log('\033[96m server listening on *:3000\033[39m');
});
大家讲道理2017-04-11 11:55:43
正确代码,结合了两位的意见,虽然你们提出了解决思路但代码不完全对,现贴上正确代码。
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 !'+
'\n\r > '+count+' other people are connected at this time.'+
'\n\r > please write your name and press enter: '
);
count++;
conn.setEncoding('utf8');
var nickname;
function broadcast(msg, exceptMyself){
for(var i in users){
if(!exceptMyself||i!=nickname){
users[i].write(msg);
}
}
}
var tmp = '';
conn.on('data',function(data){
tmp += data;
if (tmp.indexOf('\n') === -1) {
return;
}
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> '+nickname+' joined the room\033[39m\r\n');
}
}else{
broadcast('\r\n\033[96m> '+nickname+':\033[39m '+data+'\r\n');
}
tmp='';
});
conn.on('close',function(){
count--;
delete users[nickname];
broadcast('\r\n\033[096m> '+nickname+' left the room\033[39m \r\n');
});
});
server.listen(3000,function(){
console.log('\033[96m server listening on *:3000\033[39m');
});
大家讲道理2017-04-11 11:55:43
var count=0, users={};
var server=net.createServer(function(conn){
conn.write(
'\n\r > welcome to\033[92m node-chat\033[39m !'+
'\n\r > '+count+' other people are connected at this time.'+
'\n\r > please write your name and press enter: '
);
count++;
conn.setEncoding('utf8');
var nickname;
function broadcast(msg, exceptMyself){
for(var i in users){
if(!exceptMyself||i!=nickname){
users[i].write(msg);
}
}
}
conn.on('data',function(data){
data=data.replace('\r\n','');
if(!nickname){
if(users[data]){
conn.write('\r\n\033[93m> nickname already in use. try again:\033[39m\r\n ');
return;
}else{
nickname=data;
users[nickname]=conn;
broadcast('\r\n\033[90m > '+nickname+' joined the room\033[39m\r\n',true);
}
}else{
// for(var i in users){
broadcast('\r\n\033[96m> '+nickname+':\033[39m '+data+'\r\n');
// }
}
process.stdout.write(data);
});
conn.on('close',function(){
count--;
delete users[nickname];
broadcast('\r\n\033[096m> '+nickname+' left the room\033[39m \r\n');
});
});
server.listen(3000,function(){
console.log('\033[96m server listening on *:3000\033[39m');