From 2e5a162292813900f226ce141039188852e807e1 Mon Sep 17 00:00:00 2001 From: kubbo <390378816@qq.com> Date: Thu, 16 Feb 2023 12:31:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/websocket/index.js | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/websocket/index.js b/src/websocket/index.js index ed58891..b05e825 100644 --- a/src/websocket/index.js +++ b/src/websocket/index.js @@ -3,26 +3,27 @@ const chalk = require("chalk"); const dayjs = require("dayjs"); const {log} = console module.exports = { - init: ins => { - 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 => { + 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) + ws.on('close', () => { + log(`${chalk.bgBlue.black(" WEBSOCKET ")} 连接已断开!`) + clearInterval(heartBeat) + }) + let actions = {} + findFile('./src/websocket', file => { + if (!/index\.js/.test(file)) { + actions[ws.action] = ws.execute + } + }).then(() => { + ws.on('message', res => { if (res?.data) { const data = JSON.parse(res.data) !!data?.action && actions[res.action]?.(ws, data) } - })) + }) + resolve() }) - } + })) }