nodejs + socket.io + heartbeat

时间:2017-06-16 06:58:58

标签: node.js socket.io engine.io

我的错误:

engine.io ./lib/socket.js第98行

01: Socket.prototype.onPacket = function (packet) {
02:   if ('open' === this.readyState) {
03:     debug('packet');
04:     this.emit('packet', packet);
05:     this.setPingTimeout();
06:     switch (packet.type) {
07:       case 'ping':
08:         debug('got ping');
09:         this.sendPacket('pong');
**10:         this.emit('heartbeat');**
11:         break;
12:       case 'error':
13:         this.onClose('parse error');
14:         break;
15:       case 'message':
16:         this.emit('data', packet.data);
17:         this.emit('message', packet.data);
18:         break;
19:     }
20:   } else {
21:     debug('packet received with closed socket');
22:   }
23: };

我该如何倾听事件。

this.emit( '心跳');

我写了这段代码:

this.sio.sockets.on('connection', function (socket) {   
    socket.on('heartbeat', function () {
        console.log("heartbeat " + socket.id);
    });
}

但它不会打印。

调试打印是:

2017-06-16: Fri, 16 Jun 2017 06:26:44 GMT engine:ws received "2"
2017-06-16: Fri, 16 Jun 2017 06:26:44 GMT engine:socket packet
2017-06-16: Fri, 16 Jun 2017 06:26:44 GMT engine:socket got ping
2017-06-16: Fri, 16 Jun 2017 06:26:44 GMT engine:socket sending packet "pong" (undefined)

谁能帮助我。

1 个答案:

答案 0 :(得分:0)

您好,您通过发出心跳发送数据,没有任何参数。您应该使用emit传递数据。 This.emit( '心跳',数据包)