javascript - nodejs做一个简易的tcp聊天程序出现问题,运行环境是powershell,如何解决?
怪我咯
怪我咯 2017-04-11 11:53:43
0
2
516

在服务器端建立后,客户端连接后需输入用户名,但是只输入一个字符就自动传到服务器端了,还没有按回车。

另外打开两个客户端后,在其中一个客户端输入后,两个客户端都会出现两行输入,三个客户端就会出现三行输入。。。依次类推。。。。

代码如下:

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');
});
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
大家讲道理

正确代码,结合了两位的意见,虽然你们提出了解决思路但代码不完全对,现贴上正确代码。

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');
});
大家讲道理

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');
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!