在触发“断开事件”后,socket.io收到了我的自定义事件

时间:2019-01-23 02:05:32

标签: node.js socket.io

我正在使用socket.io开发一个nodejs应用程序。服务器收到“断开事件”时,会收到“ onMessage事件”吗?

有时候是可能的。我不知道为什么。

我使用地图保存套接字对象。

let my_id = 1;
let idMap = new Map();
io.sockets.on('connection',( socket ) => {
    socket.my_id = my_id++;
    idMap.set(my_id,socket);
    my_id++;
    socket.on('onMessage',( data ) => {
        let s = idMap.get(socket.my_id);
        // s is undefined
        // because it was already 'disconnect'
        console.log('onMessage client:'+socket.my_id);
    });

    socket.on('disconnect',( data ) => {
        idMap.delete(socket.my_id);
        console.log('disconnect client:'+socket.my_id);
    });
});

这是错误堆栈

rpc client error:TypeError: Cannot use 'in' operator to search for 'reLogin' in undefined
    at Socket.socket.on (/root/my_server/common/client_io.js:40:18)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at /root/my_server/node_modules/socket.io/lib/socket.js:528:12
    at args.(anonymous function) (/root/.nvm/versions/node/v8.11.3/lib/node_modules/pm2/node_modules/event-loop-inspector/index.js:133:29)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)

0 个答案:

没有答案