代码之家  ›  专栏  ›  技术社区  ›  3aQaRyAn

当计时器时间等于0时,我可以通过nestJS中的Websocket网关发送计时器吗

  •  0
  • 3aQaRyAn  · 技术社区  · 3 年前

    通过邮递员,我连接到了计时器更新事件,但当时间=0时,我仍然没有收到计时器对象

    时间服务.ts

        @Cron(CronExpression.EVERY_SECOND)
          async updateRemainingTime() {
           const timers = await this.timerModel.find({
               isActive: true,
               remainingTime: { $ne: null },
        });
    
        for (const timer of timers) {
            if (timer.isInfinite) {
                timer.remainingTime += 1;
                timer.pricePerHour += timer.price / 60;
            } else if (timer.remainingTime > 0) {
                timer.remainingTime -= 1;
                timer.pricePerHour += timer.price / 60;
                if (timer.remainingTime === 0) {
                    this.socketServer.emit('timer-updated', timer);
                }
            }
            else if(timer.remainingTime === 0) {
                await this.clear(timer._id);
                this.socketServer.emit('timer-expired', timer);
            }
    
            await timer.save();
        }
       }
    

    这是我的时间。gateway.ts

    @WebSocketGateway()
    export class TimerGateway implements OnModuleInit {
    @WebSocketServer()
    server: Server;
    
    onModuleInit() {
        this.server.on('connection', (socket) => {
            console.log(`User with ${socket.id } id is connected!`);
        });
      }
    }
    
    0 回复  |  直到 3 年前
    推荐文章