重新适配

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

View File

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