fix: socket boardcast not working
This commit is contained in:
@@ -3,6 +3,7 @@ import { createAdapter } from '@socket.io/redis-adapter'
|
||||
|
||||
import { redisSubPub } from '~/utils/redis-subpub.util'
|
||||
|
||||
export const RedisIoAdapterKey = 'mx-core-socket'
|
||||
export class RedisIoAdapter extends IoAdapter {
|
||||
createIOServer(port: number, options?: any) {
|
||||
const server = super.createIOServer(port, options)
|
||||
@@ -10,7 +11,7 @@ export class RedisIoAdapter extends IoAdapter {
|
||||
const { pubClient, subClient } = redisSubPub
|
||||
|
||||
const redisAdapter = createAdapter(pubClient, subClient, {
|
||||
key: 'mx-core-socket',
|
||||
key: RedisIoAdapterKey,
|
||||
requestsTimeout: 3000,
|
||||
})
|
||||
server.adapter(redisAdapter)
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
OnGatewayDisconnect,
|
||||
WebSocketServer,
|
||||
} from '@nestjs/websockets'
|
||||
import { Emitter } from '@socket.io/redis-emitter'
|
||||
|
||||
import { EventBusEvents } from '~/constants/event-bus.constant'
|
||||
import { AuthService } from '~/modules/auth/auth.service'
|
||||
@@ -134,8 +133,7 @@ export const createAuthGateway = (
|
||||
}
|
||||
|
||||
override broadcast(event: BusinessEvents, data: any) {
|
||||
const client = new Emitter(this.cacheService.getClient())
|
||||
client
|
||||
this.cacheService.emitter
|
||||
.of(`/${namespace}`)
|
||||
.emit('message', this.gatewayMessageFormat(event, data))
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
WebSocketGateway,
|
||||
WebSocketServer,
|
||||
} from '@nestjs/websockets'
|
||||
import { Emitter } from '@socket.io/redis-emitter'
|
||||
|
||||
import { BusinessEvents } from '~/constants/business-event.constant'
|
||||
import { RedisKeys } from '~/constants/cache.constant'
|
||||
@@ -99,7 +98,8 @@ export class WebEventsGateway
|
||||
}
|
||||
|
||||
override broadcast(event: BusinessEvents, data: any) {
|
||||
const client = new Emitter(this.cacheService.getClient())
|
||||
client.of('/web').emit('message', this.gatewayMessageFormat(event, data))
|
||||
const emitter = this.cacheService.emitter
|
||||
|
||||
emitter.of('/web').emit('message', this.gatewayMessageFormat(event, data))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@ import { Cache } from 'cache-manager'
|
||||
import { Redis } from 'ioredis'
|
||||
|
||||
import { CACHE_MANAGER, Inject, Injectable, Logger } from '@nestjs/common'
|
||||
import { Emitter } from '@socket.io/redis-emitter'
|
||||
|
||||
import { RedisIoAdapterKey } from '~/common/adapters/socket.adapter'
|
||||
import { getRedisKey } from '~/utils/redis.util'
|
||||
|
||||
// Cache 客户端管理器
|
||||
@@ -50,6 +52,18 @@ export class CacheService {
|
||||
return this.redisClient
|
||||
}
|
||||
|
||||
private _emitter: Emitter
|
||||
|
||||
public get emitter(): Emitter {
|
||||
if (this._emitter) {
|
||||
return this._emitter
|
||||
}
|
||||
this._emitter = new Emitter(this.redisClient, {
|
||||
key: RedisIoAdapterKey,
|
||||
})
|
||||
return this._emitter
|
||||
}
|
||||
|
||||
public async cleanCatch() {
|
||||
const redis = this.getClient()
|
||||
const keys: string[] = await redis.keys('mx-api-cache:*')
|
||||
|
||||
Reference in New Issue
Block a user