mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2025-07-04 14:19:19 +00:00
优化
This commit is contained in:
parent
8f61e213df
commit
0d44015957
@ -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');
|
||||
|
@ -1,5 +1,6 @@
|
||||
const relativelyPath = {
|
||||
config: '/data/crystelf/config.json',
|
||||
data: '/data/crystelf/data/',
|
||||
};
|
||||
|
||||
export default relativelyPath;
|
||||
|
@ -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}..`);
|
||||
}
|
||||
|
||||
|
@ -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 完成初始化');
|
||||
},
|
||||
};
|
||||
|
43
models/ws/handler.js
Normal file
43
models/ws/handler.js
Normal 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;
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user