日志控制

This commit is contained in:
chen wei bo
2025-09-21 19:49:11 +08:00
parent 1e63fc6ab1
commit b94d9527a2
15 changed files with 157 additions and 142 deletions
+10 -9
View File
@@ -1,5 +1,6 @@
import { InnerMsgCode } from "../Config/InnerMsgCode";
import Utils from "./Utils";
import { logger } from "db://assets/Scripts/Main/Common/Logger";
/**长连接*/
export default class SocketUnit {
@@ -18,20 +19,20 @@ export default class SocketUnit {
// 连接到服务器
public connect(url: string): void {
if (this.isConnected) {
console.log("SocketUnit: Already connected.");
logger.log("SocketUnit: Already connected.");
return;
}
this.url = url;
this.socket = new WebSocket(url);
console.log("SocketUnit: Connecting to -> " + url);
logger.log("SocketUnit: Connecting to -> " + url);
// 连接成功
this.socket.onopen = () => {
this.isConnected = true;
console.log("SocketUnit: Connection succ.");
logger.log("SocketUnit: Connection succ.");
if (this.reconnetSwt) {
console.log("SocketUnit: socket重连成功.");
logger.log("SocketUnit: socket重连成功.");
this.stopReconnect();
if (this.reSendData) {
this.sendData(this.reSendData);
@@ -42,7 +43,7 @@ export default class SocketUnit {
// 接收到消息
this.socket.onmessage = (event) => {
// console.log("SocketUnit: Received data: ", event);
// logger.log("SocketUnit: Received data: ", event);
if (this.dataReceivedCallback) {
this.dataReceivedCallback(event);
}
@@ -51,12 +52,12 @@ export default class SocketUnit {
// 连接关闭
this.socket.onclose = () => {
this.isConnected = false;
console.log("SocketUnit: Connection closed.");
logger.log("SocketUnit: Connection closed.");
};
// 发生错误
this.socket.onerror = (error) => {
console.error("SocketUnit: WebSocket error:", error);
logger.error("SocketUnit: WebSocket error:", error);
this.isConnected = false;
};
@@ -73,11 +74,11 @@ export default class SocketUnit {
// 发送数据
public sendData(data: string): void {
// console.log("SocketUnit: sendData", data);
// logger.log("SocketUnit: sendData", data);
if (this.socket && this.isConnected) {
this.socket.send(data);
} else {
console.error("SocketUnit: Socket is not connected.");
logger.error("SocketUnit: Socket is not connected.");
this.reSendData = data;
this.beginReconnect()
}