Web socket.io con AdonisJs no funciona correctamente.
Estoy usando una lib para realizar tareas dentro de adonis, adonis5-scheduler.
A continuación se muestra mi tarea.
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') } }A continuación se muestra mi 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()Llamar a Ws.io.emit desde el controlador funciona.
Llamar a Ws.io.emit desde la tarea no funciona.
Nota para la tarea Ws.io.emit no funciona, sin embargo, console.log muestra el mensaje.
La tarea no ejecuta Ws.io.emit
Resuelto
await fetch('http://127.0.0.1:3333/endpoint' para controlar
Desde el controlador llamé a emit y funcionó.