【问题标题】:nodejs + socket.io + heartbeatnodejs + socket.io + 心跳
【发布时间】:2017-06-16 17:07:11
【问题描述】:

我的错误:

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('heartbeat');

我写了这段代码:

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)

谁能帮帮我。

【问题讨论】:

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


    【解决方案1】:

    您好,您正在通过不带任何参数的发出心跳发送数据。您应该使用 emit 传递数据。 this.emit('heartbeat',packet)

    【讨论】:

      猜你喜欢
      • 2015-09-16
      • 1970-01-01
      • 2013-08-21
      • 2020-01-14
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多