重新适配

This commit is contained in:
2023-02-16 12:31:47 +08:00
parent 7693e2fda1
commit 2e5a162292

View File

@@ -3,8 +3,7 @@ const chalk = require("chalk");
const dayjs = require("dayjs");
const {log} = console
module.exports = {
init: ins => {
ins.ws('/ws', ws => {
init: ins => new Promise(resolve => ins.ws('/ws', ws => {
log(`${chalk.bgBlue.black(" WEBSOCKET ")} 服务已启动!`)
ins.send('您已成功连接到node websocket')
let heartBeat = setInterval(() => wx.send(`heartBeat at ${dayjs().format("YYYY-MM-DD HH:mm:ss")}`), 5000)
@@ -13,16 +12,18 @@ module.exports = {
clearInterval(heartBeat)
})
let actions = {}
return findFile('./src/websocket', file => {
findFile('./src/websocket', file => {
if (!/index\.js/.test(file)) {
actions[ws.action] = ws.execute
}
}).then(() => ws.on('message', res => {
}).then(() => {
ws.on('message', res => {
if (res?.data) {
const data = JSON.parse(res.data)
!!data?.action && actions[res.action]?.(ws, data)
}
}))
})
}
resolve()
})
}))
}