diff --git a/constants/path.js b/constants/path.js index 2ac80bb..c6ee14d 100644 --- a/constants/path.js +++ b/constants/path.js @@ -1,6 +1,6 @@ -import path from 'path'; import url from 'url'; import fs from 'fs'; +import path from 'path'; const __filename = url.fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -18,6 +18,7 @@ const Path = { index: path.join(rootDir, 'index.js'), pkg: path.join(rootDir, 'package.json'), yunzai: path.join(rootDir, '../../'), + data: path.join(rootDir, '../../data/crystelf/data'), }; const configFile = fs.readFileSync(Path.defaultConfig, 'utf8'); diff --git a/constants/relativelyPath.js b/constants/relativelyPath.js index 19564c5..b3215cd 100644 --- a/constants/relativelyPath.js +++ b/constants/relativelyPath.js @@ -1,5 +1,6 @@ const relativelyPath = { config: '/data/crystelf/config.json', + data: '/data/crystelf/data/', }; export default relativelyPath; diff --git a/lib/config/configControl.js b/lib/config/configControl.js index 866d172..2448fbe 100644 --- a/lib/config/configControl.js +++ b/lib/config/configControl.js @@ -5,6 +5,7 @@ import fs from 'fs'; import relativelyPath from '../../constants/relativelyPath.js'; const configPath = Path.config; +const dataPath = Path.data; const configFile = path.join(configPath, 'config.json'); const configDir = relativelyPath.config; @@ -15,6 +16,7 @@ function init() { try { if (!fs.existsSync(configPath)) { fs.mkdirSync(configPath, { recursive: true }); + fs.mkdirSync(dataPath, { recursive: true }); logger.mark(`crystelf 配置文件夹创建成功,位于 ${configPath}..`); } diff --git a/lib/system/init.js b/lib/system/init.js index 34378d3..97296e2 100644 --- a/lib/system/init.js +++ b/lib/system/init.js @@ -4,7 +4,9 @@ import wsClient from '../../models/ws/wsClient.js'; export const crystelfInit = { async CSH() { await configControl.init(); - await wsClient.initialize(); + if (configControl.get('core')) { + await wsClient.initialize(); + } logger.mark('crystelf 完成初始化'); }, }; diff --git a/models/ws/handler.js b/models/ws/handler.js new file mode 100644 index 0000000..7a6ccae --- /dev/null +++ b/models/ws/handler.js @@ -0,0 +1,43 @@ +class Handler { + constructor() { + this.handlers = new Map([ + ['auth', this.handleAuth.bind(this)], + ['ping', this.handlePing.bind(this)], + ['message', this.handleMessageFromServer.bind(this)], + ['error', this.handleError.bind(this)], + ]); + } + + async handle(client, msg) { + const handler = this.handlers.get(msg.type); + if (handler) { + await handler(client, msg); + } else { + logger.warn(`未知消息类型: ${msg.type}`); + } + } + + async handleAuth(client, msg) { + if (msg.success) { + logger.mark('crystelf WS 认证成功..'); + } else { + logger.error('crystelf WS 认证失败,关闭连接..'); + client.ws.close(4001, '认证失败'); + } + } + + async handlePing(client, msg) { + await client.sendMessage({ type: 'pong' }); + } + + async handleMessageFromServer(client, msg) { + logger.mark(`crystelf 服务端消息: ${msg.data}`); + } + + async handleError(client, msg) { + logger.warn(`crystelf WS 错误:${msg.data}`); + } +} + +const handler = new Handler(); +export default handler; diff --git a/models/ws/wsClient.js b/models/ws/wsClient.js index 1565e24..57e110f 100644 --- a/models/ws/wsClient.js +++ b/models/ws/wsClient.js @@ -1,5 +1,6 @@ import WebSocket from 'ws'; import configControl from '../../lib/config/configControl.js'; +import handler from './handler.js'; class WsClient { constructor() { @@ -23,7 +24,7 @@ class WsClient { this.clientId = configControl.get('coreConfig')?.wsClientId; this.reconnectInterval = configControl.get('coreConfig')?.wsReConnectInterval; - logger.info(this.wsURL); + //logger.info(this.wsURL); this.ws = new WebSocket(this.wsURL); this.ws.on('open', () => { @@ -34,7 +35,7 @@ class WsClient { this.ws.on('message', (raw) => { try { const data = JSON.parse(raw); - this.handleMessage(data); + handler.handle(this, data); } catch (err) { logger.err(err); } @@ -70,27 +71,6 @@ class WsClient { } } - async handleMessage(msg) { - switch (msg.type) { - case 'auth': - if (msg.success) { - logger.mark('crystelf WS 认证成功..'); - } else { - logger.error('crystelf WS 认证失败,关闭连接..'); - this.ws.close(4001, '认证失败'); - } - break; - case 'ping': - await this.sendMessage({ type: 'pong' }); - break; - case 'message': - logger.mark(`服务端消息: ${msg.data}`); - break; - default: - logger.warn(`未知消息类型: ${msg.type}`); - } - } - async reconnect() { if (this.isReconnecting) return; this.isReconnecting = true;