Web套接字。
AdonisJs不能正常工作。我正在使用一个库来执行任务在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')
}
}
下面是我的w
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显示消息。
任务未运行w.i o.emit
已解析
等待fetch('http://127.0.0.1:3333/endpoint'来控制
我从控制器调用emit,它工作了。