Node.js:TCPベースtelnet端末チャット
1384 ワード
var net=require('net'),
count = 0, //people total
users={};
var server= net.createServer(function(conn){
conn.setEncoding('utf8'); //
var nickname;
conn.write('
>Welcome to \033[91m node-chat!\033[39m'
+'
> \033[95m '+count+'\033[39m .'
+'
> : '
);
count++; //
conn.on('data',function(data){
data=data.replace('\r
','');//
if(!nickname){ //
if(users[data]){
conn.write('\033[93m> , :\033[39m');
return;
}else{
nickname=data;
users[nickname]=conn;
for(var i in users){
users[i].write('\033[90m >'+nickname+' \033[39m
');
}
}
}else{
for(var i in users){
if(i != nickname){
users[i].write('\033[94m> '+nickname+' say: \033[39m'+data+'
');
}
}
}
});
conn.on('close',function(){
count--;
delete users[nickname]; //
}); //close connection count--
});
server.listen(3000,function(){
console.log('\033[96m server listening on *:3000\033[39m');
});