【发布时间】: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