[英]@socket.io/redis-adapter and redis; sub client doesn't reconnect after error
鉴于我正在导入这些包:
import {Socket, Server} from 'socket.io'; // @4.4.1
import {createClient} from 'redis'; // @4.0.4
import {createAdapter} from '@socket.io/redis-adapter'; // @7.1.0
import {Server as HttpServer} from 'http'; // npm@8.5.5, node@v17.7.0
我有一个我创建的服务器:
const http = HttpServer // from 'http', creation not in this code
const io = new Server(http, {
path: '/socket/v1/tunnel',
serveClient: false,
cookie: false,
pingTimeout: 295000,
pingInterval: 300000,
});
和我创建的 Redis 个发布/订阅客户端:
const pubClient = createClient({
url: redisAddress,
});
const subClient = pubClient.duplicate();
在我拥有这些之后,我连接 Redis 客户端并将它们添加到适配器(在io
中),如下所示:
Promise.all([pubClient.connect(), subClient.connect()]).then(() => {
io.adapter(createAdapter(pubClient, subClient));
...
}
有了上面的内容,我就有了一个可以工作的套接字服务器。 我的问题是每隔约 1 小时我与 Redis 客户端的连接就会出错。 pubClient 立即重新连接(正如我预期的那样,按照文档),但 subClient 出错并且不会重新连接。
我可以从其他系统向 Redis 中的房间发送消息,当此子客户端已连接时,消息会传递到适当的设备。 在 subClient 第一次出错后(它在大约 1 小时的间隔内相当可预测)它不会重新连接,这意味着发送到 Redis 的房间不再被路由到它们的目的地。
我有几个问题:
subClient.on('error', () => {...}
的处理程序中添加一个subClient.connect()
,还是我配置不正确并且应该使用 createClient({. ..})?此问题是由 redis package 引起的。redis@4 未正确重新连接发布/订阅频道(当前为 v4.0.4)。 将 redis 降级到 v3.1.2 完全解决了重新连接问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.