修复打包
This commit is contained in:
@@ -1,29 +1,34 @@
|
|||||||
const {findFile} = require("../utils/fsUtils");
|
const {findFile} = require("../utils/fsUtils");
|
||||||
const chalk = require("chalk");
|
const chalk = require("chalk");
|
||||||
const dayjs = require("dayjs");
|
const dayjs = require("dayjs");
|
||||||
|
const {checkJson} = require("../tools");
|
||||||
const {log} = console
|
const {log} = console
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init: ins => new Promise(resolve => ins.ws('/ws', ws => {
|
init: ins => new Promise(resolve => {
|
||||||
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 = {}
|
let actions = {}
|
||||||
findFile('./src/websocket', file => {
|
findFile('./src/websocket', file => {
|
||||||
if (!/index\.js/.test(file)) {
|
if (!/index\.js/.test(file)) {
|
||||||
actions[ws.action] = ws.execute
|
const item = require(file.replace(/src[\\\/]websocket/, '.'))
|
||||||
|
actions[item.action] = item.execute
|
||||||
}
|
}
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
|
ins.ws('/ws', ws => {
|
||||||
|
log(`${chalk.bgBlue.black(" WEBSOCKET ")} 服务已启动!`)
|
||||||
|
ws.send('您已成功连接到node websocket')
|
||||||
|
ws.onerror = () => ws.close()
|
||||||
|
ws.on('close', () => {
|
||||||
|
log(`${chalk.bgRed.black(" WEBSOCKET ")} 连接已断开!`)
|
||||||
|
clearInterval(heartBeat)
|
||||||
|
})
|
||||||
ws.on('message', res => {
|
ws.on('message', res => {
|
||||||
if (res?.data) {
|
if (checkJson(res)) {
|
||||||
const data = JSON.parse(res.data)
|
const data = JSON.parse(res)
|
||||||
!!data?.action && actions[res.action]?.(ws, data)
|
!!data?.action && actions[data.action]?.(ws, data)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
let heartBeat = setInterval(() => ws.send(`heartBeat at ${dayjs().format("YYYY-MM-DD HH:mm:ss")}`), 5000)
|
||||||
resolve()
|
resolve()
|
||||||
})
|
})
|
||||||
}))
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user