nodejs学习----构建一个简单的TCP聊天服务器

这里给出一个适合初学者的实例:TCP聊天服务器

源码下载地址:https://files.cnblogs.com/joyho/app.zip

 

第一步:实例化服务器,服务器error抛出,记录关闭信息,将服务器绑定到端口4001

var net = require('net');
var server = net.createServer();

server.on('error',function(err){
    console.log('Server error:',err.message);
});

server.on('close',function(){
    console.log('Server closed');

});

server.listen(4001);

 

第二步:接受客户端连接

var net = require('net');
var server = net.createServer();

server.on('connection',function(socket){
    console.log('got a new connection');
});

server.on('error',function(err){
    console.log('Server error:',err.message);
});

server.on('close',function(){
    console.log('Server closed');

});

server.listen(4001);

 

第三步:从连接中读取数据

var net = require('net');
var server = net.createServer();

server.on('connection',function(socket){
    console.log('got a new connection');
    
    socket.on('data',function(data){
        console.log('got data:',data);
     });
});

server.on('error',function(err){
    console.log('Server error:',err.message);
});

server.on('close',function(){
    console.log('Server closed');

});

server.listen(4001);    

 

第四步:聚合所有客户端

var net = require('net');
var server = net.createServer();

var sockets = [];

server.on('connection',function(socket){
    console.log('got a new connection');
   
    sockets.push(socket);
    
    socket.on('data',function(data){
        console.log('got data:',data);
     });
});

server.on('error',function(err){
    console.log('Server error:',err.message);
});

server.on('close',function(){
    console.log('Server closed');

});

server.listen(4001); 

 

第五步:广播数据

var net = require('net');
var server = net.createServer();

var sockets = [];

server.on('connection',function(socket){
    console.log('got a new connection');
   
    sockets.push(socket);
    
    socket.on('data',function(data){
        console.log('got data:',data);

        sockets.forEach(function(otherSocket){
            if(otherSocket != socket){
                otherSocket.write(data);
            }
        });
     });
});

server.on('error',function(err){
    console.log('Server error:',err.message);
});

server.on('close',function(){
    console.log('Server closed');

});

server.listen(4001);

 

第六步:删除被关闭的连接

var net = require('net');
var server = net.createServer();
var sockets = [];

server.on('connection',function(socket){
    console.log('got a new connection');

    sockets.push(socket);

    socket.on('data',function(data){
        console.log('got data:',data);

        sockets.forEach(function(otherSocket){
            if(otherSocket != socket){
                otherSocket.write(data);
            }
        });

    });

    socket.on('close',function(){
        console.log('connection closed');
        var index = sockets.indexOf(socket);
        sockets.splice(index,1);
    });

});

server.on('error',function(err){
    console.log('Server error:',err.message);
});

server.on('close',function(){
    console.log('Server closed');

});

server.listen(4001);

 

第七步:测试运行

1.文件保存为app.js

2.打开cmd窗口,进入app.js所在的目录,输入命令:node app.js  , 回车

3.使用telnet连接服务器:打开cmd窗口,运行telnet localhost 4001命令。(重复该步骤几次,创建多个客户端连接服务器)

4.在步骤3中打开的窗口中任选一个输入随便一串字符,在其他窗口中也能够看到该字符串。

posted @ 2014-08-09 00:56  Joy Ho  阅读(806)  评论(0编辑  收藏  举报