mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 14:49:19 +00:00
适配crystelf-plugin
添加reportBots支持
This commit is contained in:
parent
d8d7fc864b
commit
d442d707d3
@ -17,7 +17,6 @@ class RedisService {
|
||||
private async initialize() {
|
||||
await this.connectWithRetry();
|
||||
this.setupEventListeners();
|
||||
//await this.test();
|
||||
}
|
||||
|
||||
private async connectWithRetry(): Promise<void> {
|
||||
@ -47,6 +46,9 @@ class RedisService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Redis客户端事件监听器
|
||||
*/
|
||||
private setupEventListeners(): void {
|
||||
this.client.on('error', (err) => {
|
||||
if (!err.message.includes('ECONNREFUSED')) {
|
||||
@ -80,6 +82,11 @@ class RedisService {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Redis客户端实例
|
||||
* @returns {Redis} Redis客户端
|
||||
* @throws 如果未连接,则记录fatal日志
|
||||
*/
|
||||
public getClient(): Redis {
|
||||
if (!this.isConnected) {
|
||||
logger.fatal(1, 'Redis未连接..');
|
||||
@ -87,11 +94,22 @@ class RedisService {
|
||||
return this.client;
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开Redis连接
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async disconnect(): Promise<void> {
|
||||
await this.client.quit();
|
||||
this.isConnected = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储对象到Redis
|
||||
* @template T
|
||||
* @param {string} key Redis键
|
||||
* @param {T} value 要存储的对象
|
||||
* @param {number} [ttl] 过期时间(秒)
|
||||
*/
|
||||
public async setObject<T>(key: string, value: T, ttl?: number): Promise<void> {
|
||||
const serialized = redisTools.serialize(value);
|
||||
await this.getClient().set(key, serialized);
|
||||
@ -101,14 +119,27 @@ class RedisService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Redis获取对象
|
||||
* @template T
|
||||
* @param {string} key Redis键
|
||||
* @returns {Promise<T | undefined>} 获取到的对象或undefined
|
||||
*/
|
||||
public async getObject<T>(key: string): Promise<T | undefined> {
|
||||
const serialized = await this.getClient().get(key);
|
||||
if (!serialized) return undefined;
|
||||
|
||||
const deseralized = redisTools.deserialize<T>(serialized);
|
||||
return redisTools.reviveDates(deseralized);
|
||||
const deserialized = redisTools.deserialize<T>(serialized);
|
||||
return redisTools.reviveDates(deserialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Redis中已存在的对象
|
||||
* @template T
|
||||
* @param {string} key Redis键
|
||||
* @param {T} updates 更新内容
|
||||
* @returns {Promise<T>} 更新后的对象
|
||||
*/
|
||||
public async update<T>(key: string, updates: T): Promise<T> {
|
||||
const existing = await this.getObject<T>(key);
|
||||
if (!existing) {
|
||||
@ -119,6 +150,13 @@ class RedisService {
|
||||
return updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Redis或本地文件获取数据
|
||||
* @template T
|
||||
* @param {string} key Redis键
|
||||
* @param {string} fileName 本地文件名
|
||||
* @returns {Promise<T | undefined>} 获取到的数据或undefined
|
||||
*/
|
||||
public async fetch<T>(key: string, fileName: string): Promise<T | undefined> {
|
||||
const data = await this.getObject<T>(key);
|
||||
if (data) return data;
|
||||
@ -130,12 +168,22 @@ class RedisService {
|
||||
logger.error(`数据${key}不存在..`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据持久化到Redis和本地文件
|
||||
* @template T
|
||||
* @param {string} key Redis键
|
||||
* @param {T} data 要持久化的数据
|
||||
* @param {string} fileName 本地文件名
|
||||
*/
|
||||
public async persistData<T>(key: string, data: T, fileName: string): Promise<void> {
|
||||
await this.setObject(key, data);
|
||||
await Persistence.writeDataLocal(key, data, fileName);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试方法:示例性地获取一个用户数据
|
||||
*/
|
||||
public async test(): Promise<void> {
|
||||
const user = await this.fetch<IUser>('Jerry', 'IUser');
|
||||
logger.debug('User:', user);
|
||||
|
@ -2,6 +2,7 @@ import { AuthenticatedSocket } from '../../types/ws';
|
||||
import wsTools from '../../utils/ws/wsTools';
|
||||
import { WebSocket } from 'ws';
|
||||
import logger from '../../utils/core/logger';
|
||||
import redisService from '../redis/redis';
|
||||
|
||||
type MessageHandler = (socket: WebSocket, msg: any) => Promise<void>;
|
||||
|
||||
@ -13,6 +14,7 @@ class WSMessageHandler {
|
||||
['test', this.handleTest],
|
||||
['ping', this.handlePing],
|
||||
['pong', this.handlePong],
|
||||
['reportBots', this.handleReportBots],
|
||||
]);
|
||||
}
|
||||
|
||||
@ -48,7 +50,7 @@ class WSMessageHandler {
|
||||
}
|
||||
|
||||
private async handlePong(socket: WebSocket, msg: any) {
|
||||
logger.debug(`received pong ${msg.data}`);
|
||||
logger.debug(`received pong`);
|
||||
}
|
||||
|
||||
private async handleUnknown(socket: WebSocket, msg: any) {
|
||||
@ -59,6 +61,11 @@ class WSMessageHandler {
|
||||
});
|
||||
}
|
||||
|
||||
private async handleReportBots(socket: WebSocket, msg: any) {
|
||||
logger.debug(`received reportBots: ${msg.data}`);
|
||||
await redisService.persistData('crystelf', msg, 'bots');
|
||||
}
|
||||
|
||||
public registerHandler(type: string, handler: MessageHandler): void {
|
||||
this.handlers.set(type, handler);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user