client 无法监听connect事件?

var events = require('events');

var net = require('net');

var channel = new events.EventEmitter();

channel.clients = {};

channel.subscriptions = {};


channel.on('join',function(id,client){

    console.log('connect');

    this.clients[id] = client;

    this.subscriptions[id] = function(senderId,message){

        console.log(message);

        if(id != senderId){

            this.clients[id].write(message);

        }

    };

    this.on('broadcast',this.subscriptions[id]);

});


var server =  net.createServer(function(client){

    var id = client.remoteAddress+':'+client.remotePort;

    client.on('connect',function(){

        console.log('client connect');

        channel.emit('join',id,client);

    });

    client.on('data',function(data){

        data = data.toString();

        console.log(data);

        channel.emit('broadcast',id,data);

    });

});

server.listen(8887);


这句始终不被执行,为什么? client.on('connect',function(){ console.log('client connect'); channel.emit('join',id,client); });


Helenr
浏览 929回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript