0

我的错误:

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)

谁能帮我。

4

1 回答 1

1

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

于 2017-06-16T09:07:11.517 回答