如何使用socket.io将数据从服务器发送到客户端

时间:2017-03-16 13:50:12

标签: javascript node.js sockets socket.io

我有一个名为app.js的node.js文件和一个名为index.html的html文件。当我运行服务器时,服务器给我一些增量号码。我想将这些号码从服务器发送到客户端但是我收到一个名为的错误(节点:4620)MaxListenersExceededWarning:检测到可能的EventEmitter内存泄漏。添加了11个连接侦听器。使用emitter.setMaxListeners()来增加限制。我不知道如何解决这个问题,请帮帮我。

/app.js

server.on('published', function(packet, client) {
  console.log('Published', packet.payload);
  //console.log('client', client);

  sendMessageToClient(packet) // Incremental value send to these function
});

function   sendMessageToClient(packet) {

    var count = packet.payload.toString('utf-8'); //store the incremental value to the count

    io.sockets.on('connection', function(socket){
                console.log(count);
        socket.emit('message', count);
    });

}

/index.html

 <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
    <script>
                var socket = io.connect('localhost:5000');
                socket.on('message', function(data){
                console.log(data.message);
                });
    </script>

0 个答案:

没有答案