NestJS 网关依赖注入
NestJS Gateway dependency injection
我有一个名为“PlayerService”的服务,网关称为“Player Gateway”,PlayersGateway 依赖于 PlayerService,但我无法在网关内注入服务。
玩家网关
@UseInterceptors(RedisPropagatorInterceptor)
@WebSocketGateway()
@Injectable()
export class PlayerGateway
extends BaseGateway
implements OnGatewayConnection, OnGatewayInit, OnGatewayDisconnect {
constructor(private playerService: PlayerService) {
super();
}
播放器服务
@Injectable()
export class PlayerService extends BaseService<Player> {
protected fieldsSearchable: FieldsSearchable = {};
private logger = new Logger('PlayerService');
constructor(
@InjectRepository(PlayerRepository)
repository: PlayerRepository,
@Inject(REQUEST) protected request: Request,
private redisService: RedisService,
) {
super();
this.setRepository(repository);
}
播放器模块
@Module({
imports: [
TypeOrmModule.forFeature([Player, PlayerRepository]),
RedisModule,
SocketModule,
RedisPropagatorModule,
],
controllers: [PlayerController],
exports: [TypeOrmModule, PlayerService],
providers: [PlayerService, PlayerGateway],
})
export class PlayerModule {}
Modules.ts
@Module({
imports: [
RedisModule,
SocketModule,
RedisPropagatorModule,
SessionModule,
PlayerModule,
],
exports: [RedisModule, SocketModule],
controllers: [],
providers: [ServerGateway, NetworkGateway],
})
我想知道的是如何将服务注入网关或制作类似这样的 rsrs
您的 PlayerService 正在尝试注入请求:
@Inject(REQUEST) protected request: Request
这对于构建网关实例没有意义,因为它应该独立于任何单独的 HTTP 请求。您真的需要访问 PlayerService
中的请求吗?如果是这样,您必须将其拆分为单独的服务
我有一个名为“PlayerService”的服务,网关称为“Player Gateway”,PlayersGateway 依赖于 PlayerService,但我无法在网关内注入服务。
玩家网关
@UseInterceptors(RedisPropagatorInterceptor)
@WebSocketGateway()
@Injectable()
export class PlayerGateway
extends BaseGateway
implements OnGatewayConnection, OnGatewayInit, OnGatewayDisconnect {
constructor(private playerService: PlayerService) {
super();
}
播放器服务
@Injectable()
export class PlayerService extends BaseService<Player> {
protected fieldsSearchable: FieldsSearchable = {};
private logger = new Logger('PlayerService');
constructor(
@InjectRepository(PlayerRepository)
repository: PlayerRepository,
@Inject(REQUEST) protected request: Request,
private redisService: RedisService,
) {
super();
this.setRepository(repository);
}
播放器模块
@Module({
imports: [
TypeOrmModule.forFeature([Player, PlayerRepository]),
RedisModule,
SocketModule,
RedisPropagatorModule,
],
controllers: [PlayerController],
exports: [TypeOrmModule, PlayerService],
providers: [PlayerService, PlayerGateway],
})
export class PlayerModule {}
Modules.ts
@Module({
imports: [
RedisModule,
SocketModule,
RedisPropagatorModule,
SessionModule,
PlayerModule,
],
exports: [RedisModule, SocketModule],
controllers: [],
providers: [ServerGateway, NetworkGateway],
})
我想知道的是如何将服务注入网关或制作类似这样的 rsrs
您的 PlayerService 正在尝试注入请求:
@Inject(REQUEST) protected request: Request
这对于构建网关实例没有意义,因为它应该独立于任何单独的 HTTP 请求。您真的需要访问 PlayerService
中的请求吗?如果是这样,您必须将其拆分为单独的服务