chore: 日志美化

This commit is contained in:
手瓜一十雪 2024-08-12 00:08:49 +08:00
parent 9afc0f6667
commit ecee642e10

View File

@ -39,6 +39,8 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
//鉴权 //鉴权
this.authorize(token, wsClient, wsReq); this.authorize(token, wsClient, wsReq);
wsClient.on('error', (err) => this.logger.log('[OneBot] [WebSocket Server] Client Error:', err.message));
wsClient.on('message', (message) => { wsClient.on('message', (message) => {
this.handleMessage(wsClient, message).then().catch(this.logger.logError); this.handleMessage(wsClient, message).then().catch(this.logger.logError);
}); });
@ -53,7 +55,7 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
await this.wsClientsMutex.runExclusive(async () => { await this.wsClientsMutex.runExclusive(async () => {
this.wsClients.push(wsClient); this.wsClients.push(wsClient);
}); });
}); }).on('error', (err) => this.logger.log('[OneBot] [WebSocket Server] Server Error:', err.message));
} }
registerActionMap(actionMap: Map<string, BaseAction<any, any>>) { registerActionMap(actionMap: Map<string, BaseAction<any, any>>) {
@ -67,25 +69,25 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
onEvent<T extends OB11EmitEventContent>(event: T) { onEvent<T extends OB11EmitEventContent>(event: T) {
this.wsClientsMutex.runExclusive(async () => { this.wsClientsMutex.runExclusive(async () => {
this.wsClients.forEach((wsClient) => { this.wsClients.forEach((wsClient) => {
const wrappedEvent = this.wrapEvent(event); wsClient.send(JSON.stringify(event));
wsClient.send(JSON.stringify(wrappedEvent));
}); });
}); });
} }
open() { open() {
if (this.hasBeenClosed) { if (this.isOpen) {
this.logger.logError('Cannot open a closed WebSocket server'); this.logger.logError('[OneBot] [WebSocket Server] Cannot open a opened WebSocket server');
return; return;
} }
this.logger.log('WebSocket server started', this.wsServer.address()); let addressInfo = this.wsServer.address();
this.logger.log('[OneBot] [WebSocket Server] Server Started', typeof (addressInfo) === 'string' ? addressInfo : addressInfo?.address + ':' + addressInfo?.port);
this.isOpen = true; this.isOpen = true;
this.registerHeartBeat(); this.registerHeartBeat();
} }
async close() { async close() {
this.isOpen = false; this.isOpen = false;
this.hasBeenClosed = true;
this.wsServer.close(); this.wsServer.close();
if (this.heartbeatIntervalId) { if (this.heartbeatIntervalId) {
clearInterval(this.heartbeatIntervalId); clearInterval(this.heartbeatIntervalId);
@ -117,14 +119,6 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
wsClient.close(); wsClient.close();
} }
private async checkStateAndAnnounce<T>(data: T) {
await this.wsClientsMutex.runExclusive(async () => {
this.wsClients.forEach((wsClient) => {
this.checkStateAndReply(data, wsClient);
});
});
}
private checkStateAndReply<T>(data: T, wsClient: WebSocket) { private checkStateAndReply<T>(data: T, wsClient: WebSocket) {
if (wsClient.readyState === WebSocket.OPEN) { if (wsClient.readyState === WebSocket.OPEN) {
wsClient.send(JSON.stringify(data)); wsClient.send(JSON.stringify(data));
@ -138,7 +132,7 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
try { try {
receiveData = JSON.parse(message.toString()); receiveData = JSON.parse(message.toString());
echo = receiveData.echo; echo = receiveData.echo;
this.logger.logDebug('收到正向Websocket消息', receiveData); //this.logger.logDebug('收到正向Websocket消息', receiveData);
} catch (e) { } catch (e) {
this.checkStateAndReply<any>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo), wsClient); this.checkStateAndReply<any>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo), wsClient);
} }
@ -151,11 +145,5 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
} }
} }
private wrapEvent<T extends OB11EmitEventContent>(event: T) {
return {
type: 'event',
data: event,
};
}
} }