重新调整websocket的配置

This commit is contained in:
2023-02-16 10:34:05 +08:00
parent 1bc5e3b9c4
commit 518efac58a
2 changed files with 45 additions and 44 deletions

View File

@@ -1,14 +1,27 @@
const {findFile} = require("../utils/fsUtils");
const chalk = require("chalk");
const dayjs = require("dayjs");
const {log} = console
module.exports = {
init: ins => {
return findFile('./src/websocket', file => {
if (!/index\.js/.test(file)) {
const ws = require(file.replace(/src[\\\/]websocket/, '.'))
log(`${chalk.bgBlue.black(" WEBSOCKET ")} ${ws.action}`)
ins.ws(ws.action, ws.execute)
}
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)
ws.on('close', () => {
log(`${chalk.bgBlue.black(" WEBSOCKET ")} 连接已断开!`)
clearInterval(heartBeat)
})
let actions = {}
return findFile('./src/websocket', file => {
if (!/index\.js/.test(file)) {
actions[ws.action] = ws.execute
}
}).then(() => ws.on('message', res => {
if (res?.action) {
actions[res.action]?.(ws, JSON.parse(res))
}
}))
})
}
}