另一个'听众必须是一个功能'错误。 我仔细检查了所有参数,检查了正确的语法等但仍无法找到问题所在:
以下是我的代码片段。
在login.js中:
global.eventEmitter = require('./helpers/EventEmitter.js');
var packetSwitch = require('./handlers/PacketSwitch.js');
global.ackHandler = require('./handlers/ACKPacketHandler.js');
...
global.eventEmitter.createAndFireEvent('opCodeReceived',packetSwitch.switch(opcode, decrypted, sock));
在PacketSwitch.js中:
var packetHandler = require('./PacketHandler.js');
exports.switch = function(opcode, data, sock) {
switch (opcode) {
case 1:
eventEmitter.createAndFireEvent('reqLoginReceived', packetHandler.handleLoginReq(data, sock));
break;
case 2:
eventEmitter.createAndFireEvent('reqDateReceived', packetHandler.handleDateReq(data, sock));
break;
default:
eventEmitter.createAndFireEvent('unknownReceived', packetHandler.handleUnknown(data, sock));
}
};
让我们假设它进入案例2: 在PacketHandler.js中:
exports.handleDateReq = function(data, sock) {
global.eventEmitter.createAndFireEvent('handleDateAck', global.ackHandler.handleDateAck(sock));
};
在ACKPacketHandler.js中:
exports.handleDateAck = function(sock) {
sock.write("Test!");
};
在EventEmitter.js中:
var eventEmitter = require('events');
const myEmitter = new eventEmitter.EventEmitter();
exports.createAndFireEvent = function(event, callback) {
myEmitter.on(event, callback);
myEmitter.emit(event);
};
这是我得到的错误的屏幕截图: https://gyazo.com/6047c5917f937aa33f29f9fc40cdf5f7
答案 0 :(得分:2)
您的数据包处理程序函数(例如handleDateAck()
)不是返回函数。事实上,他们似乎没有回来任何东西。当您将侦听器函数传递给emitter.on()
时,返回值是您当前正在使用的值。