我最近从 Socket.io 2.x 升级到 3.0.3 并将它与 socket-io-redis@6.0.1 一起使用。
根据原始迁移文档中的解释,我正在尝试获取所有打开套接字的列表:
const ids = await io.allSockets();
这会导致以下错误:
(node:20818) UnhandledPromiseRejectionWarning: Error: timeout reached while waiting for sockets response
at Timeout._onTimeout (/var/app/node_modules/socket.io-redis/dist/index.js:286:28)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7)
(node:20818) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)
请注意,只有少数客户端连接到我的开发环境,因此它试图处理太多可能导致超时的数据。
当我尝试在下面运行时,我得到了同样的错误:
const sockets = await io.of('/').adapter.sockets();
console.log(sockets); // a Set containing all the connected socket ids
const sockets = await io.of('/').adapter.sockets(new Set(['room1', 'room2']));
console.log(sockets); // a Set containing the socket ids in 'room1' or in 'room2'
// this method is also exposed by the Server instance
const sockets = io.in('room3').allSockets();
console.log(sockets); // a Set containing the socket ids in 'room3'
如果有人对导致问题的原因以及如何解决问题有任何想法,将不胜感激。
最好的
H