Co-authored-by: Hongtao Lye <codert.sn@gmail.com> Co-authored-by: liuyi <forehalo@gmail.com> Co-authored-by: LongYinan <lynweklm@gmail.com> Co-authored-by: X1a0t <405028157@qq.com> Co-authored-by: JimmFly <yangjinfei001@gmail.com> Co-authored-by: Peng Xiao <pengxiao@outlook.com> Co-authored-by: xiaodong zuo <53252747+zuoxiaodong0815@users.noreply.github.com> Co-authored-by: DarkSky <25152247+darkskygit@users.noreply.github.com> Co-authored-by: Qi <474021214@qq.com> Co-authored-by: danielchim <kahungchim@gmail.com>
38 lines
1006 B
TypeScript
38 lines
1006 B
TypeScript
import { IoAdapter } from '@nestjs/platform-socket.io';
|
|
import { createAdapter } from '@socket.io/redis-adapter';
|
|
import { Redis } from 'ioredis';
|
|
import { ServerOptions } from 'socket.io';
|
|
|
|
export class RedisIoAdapter extends IoAdapter {
|
|
private adapterConstructor: ReturnType<typeof createAdapter> | undefined;
|
|
|
|
async connectToRedis(
|
|
host: string,
|
|
port: number,
|
|
username: string,
|
|
password: string,
|
|
db: number
|
|
): Promise<void> {
|
|
const pubClient = new Redis(port, host, {
|
|
username,
|
|
password,
|
|
db,
|
|
});
|
|
pubClient.on('error', err => {
|
|
console.error(err);
|
|
});
|
|
const subClient = pubClient.duplicate();
|
|
subClient.on('error', err => {
|
|
console.error(err);
|
|
});
|
|
|
|
this.adapterConstructor = createAdapter(pubClient, subClient);
|
|
}
|
|
|
|
override createIOServer(port: number, options?: ServerOptions): any {
|
|
const server = super.createIOServer(port, options);
|
|
server.adapter(this.adapterConstructor);
|
|
return server;
|
|
}
|
|
}
|