This commit is contained in:
Jerry 2025-04-27 13:58:00 +08:00
parent 8f61e213df
commit 0d44015957
6 changed files with 54 additions and 25 deletions

View File

@ -1,6 +1,6 @@
import path from 'path';
import url from 'url'; import url from 'url';
import fs from 'fs'; import fs from 'fs';
import path from 'path';
const __filename = url.fileURLToPath(import.meta.url); const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
@ -18,6 +18,7 @@ const Path = {
index: path.join(rootDir, 'index.js'), index: path.join(rootDir, 'index.js'),
pkg: path.join(rootDir, 'package.json'), pkg: path.join(rootDir, 'package.json'),
yunzai: path.join(rootDir, '../../'), yunzai: path.join(rootDir, '../../'),
data: path.join(rootDir, '../../data/crystelf/data'),
}; };
const configFile = fs.readFileSync(Path.defaultConfig, 'utf8'); const configFile = fs.readFileSync(Path.defaultConfig, 'utf8');

View File

@ -1,5 +1,6 @@
const relativelyPath = { const relativelyPath = {
config: '/data/crystelf/config.json', config: '/data/crystelf/config.json',
data: '/data/crystelf/data/',
}; };
export default relativelyPath; export default relativelyPath;

View File

@ -5,6 +5,7 @@ import fs from 'fs';
import relativelyPath from '../../constants/relativelyPath.js'; import relativelyPath from '../../constants/relativelyPath.js';
const configPath = Path.config; const configPath = Path.config;
const dataPath = Path.data;
const configFile = path.join(configPath, 'config.json'); const configFile = path.join(configPath, 'config.json');
const configDir = relativelyPath.config; const configDir = relativelyPath.config;
@ -15,6 +16,7 @@ function init() {
try { try {
if (!fs.existsSync(configPath)) { if (!fs.existsSync(configPath)) {
fs.mkdirSync(configPath, { recursive: true }); fs.mkdirSync(configPath, { recursive: true });
fs.mkdirSync(dataPath, { recursive: true });
logger.mark(`crystelf 配置文件夹创建成功,位于 ${configPath}..`); logger.mark(`crystelf 配置文件夹创建成功,位于 ${configPath}..`);
} }

View File

@ -4,7 +4,9 @@ import wsClient from '../../models/ws/wsClient.js';
export const crystelfInit = { export const crystelfInit = {
async CSH() { async CSH() {
await configControl.init(); await configControl.init();
await wsClient.initialize(); if (configControl.get('core')) {
await wsClient.initialize();
}
logger.mark('crystelf 完成初始化'); logger.mark('crystelf 完成初始化');
}, },
}; };

43
models/ws/handler.js Normal file
View File

@ -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;

View File

@ -1,5 +1,6 @@
import WebSocket from 'ws'; import WebSocket from 'ws';
import configControl from '../../lib/config/configControl.js'; import configControl from '../../lib/config/configControl.js';
import handler from './handler.js';
class WsClient { class WsClient {
constructor() { constructor() {
@ -23,7 +24,7 @@ class WsClient {
this.clientId = configControl.get('coreConfig')?.wsClientId; this.clientId = configControl.get('coreConfig')?.wsClientId;
this.reconnectInterval = configControl.get('coreConfig')?.wsReConnectInterval; this.reconnectInterval = configControl.get('coreConfig')?.wsReConnectInterval;
logger.info(this.wsURL); //logger.info(this.wsURL);
this.ws = new WebSocket(this.wsURL); this.ws = new WebSocket(this.wsURL);
this.ws.on('open', () => { this.ws.on('open', () => {
@ -34,7 +35,7 @@ class WsClient {
this.ws.on('message', (raw) => { this.ws.on('message', (raw) => {
try { try {
const data = JSON.parse(raw); const data = JSON.parse(raw);
this.handleMessage(data); handler.handle(this, data);
} catch (err) { } catch (err) {
logger.err(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() { async reconnect() {
if (this.isReconnecting) return; if (this.isReconnecting) return;
this.isReconnecting = true; this.isReconnecting = true;