簡體   English   中英

帶有 AdonisJs 的 Web socket.io 無法正常工作

[英]Web socket.io with AdonisJs not working properly

帶有 AdonisJs 的 Web socket.io 無法正常工作。

我正在使用一個 lib 在 adonis、adonis5-scheduler 中執行任務。

下面是我的任務。

import { BaseTask } from 'adonis5-scheduler/build'

export default class GetRouletteGame extends BaseTask {
    public static get schedule() {
        return '*/3 * * * * *'
    }
    /**
     * Set enable use .lock file for block run retry task
     * Lock file save to `build/tmpTaskLock`
     */
    public static get useLock() {
        return false
    }

    public async handle() {
      Ws.io.emit(`taskName`, 1)
      console.log('ok')
    }
}

下面是我的WS。

import { Server } from 'socket.io'
import AdonisServer from '@ioc:Adonis/Core/Server'

class Ws {
  public io: Server
  private booted = false

  public boot() {
    /**
     * Ignore multiple calls to the boot method
     */
    if (this.booted) {
      return
    }

    this.booted = true
    this.io = new Server(AdonisServer.instance!, {
      cors: {
        origin: '*'
      }
    })
  }
}

export default new Ws()

從控制器調用 Ws.io.emit 有效。

從任務中調用 Ws.io.emit 不起作用。

注意任務 Ws.io.emit 不起作用,但 console.log 會顯示該消息。

任務不運行 Ws.io.emit

解決

await fetch('http://127.0.0.1:3333/endpoint' 來控制

從我調用的控制器中發出並且它起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM