fix: listener proxy

This commit is contained in:
linyuchen
2024-04-27 17:04:53 +08:00
parent 768cef648d
commit c9b47b41e6
7 changed files with 32 additions and 15 deletions

View File

@@ -53,12 +53,12 @@ export abstract class HttpServerBase {
next();
}
start(port: number) {
start(port: number, host: string) {
try {
this.expressAPP.get('/', (req: Request, res: Response) => {
res.send(`${this.name}已启动`);
});
this.listen(port);
this.listen(port, host);
} catch (e: any) {
logError('HTTP服务启动失败', e.toString());
// llonebotError.httpServerError = "HTTP服务启动失败, " + e.toString()
@@ -73,9 +73,9 @@ export abstract class HttpServerBase {
}
}
restart(port: number) {
restart(port: number, host: string) {
this.stop();
this.start(port);
this.start(port, host);
}
abstract handleFailed(res: Response, payload: any, err: any): void
@@ -108,9 +108,10 @@ export abstract class HttpServerBase {
});
}
protected listen(port: number) {
this.server = this.expressAPP.listen(port, '0.0.0.0', () => {
const info = `${this.name} started 0.0.0.0:${port}`;
protected listen(port: number, host: string = '0.0.0.0') {
host = host || '0.0.0.0';
this.server = this.expressAPP.listen(port, host, () => {
const info = `${this.name} started ${host}:${port}`;
log(info);
});
}

View File

@@ -27,12 +27,14 @@ export class WebsocketServerBase {
constructor() {
}
start(port: number) {
start(port: number, host: string = '') {
try {
this.ws = new WebSocketServer({
port ,
host: '',
maxPayload: 1024 * 1024 * 1024
});
log(`ws服务启动成功, ${host}:${port}`);
} catch (e: any) {
throw Error('ws服务启动失败, ' + e.toString());
}

View File

@@ -36,6 +36,9 @@ export class ConfigBase<T>{
const data = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
logDebug(`配置文件${configPath}已加载`, data);
Object.assign(this, data);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
this.save(this); // 保存一次,让新版本的字段写入
return this;
} catch (e: any) {
if (e instanceof SyntaxError) {
@@ -48,9 +51,10 @@ export class ConfigBase<T>{
}
save(config: T) {
Object.assign(this, config);
const configPath = this.getConfigPath();
try {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
fs.writeFileSync(configPath, JSON.stringify(this, null, 2));
} catch (e: any) {
logError(`保存配置文件 ${configPath} 时发生错误:`, e.message);
}