From 2bde59e5cc53ea9e3d645fb188ee1027ed6a7439 Mon Sep 17 00:00:00 2001 From: MoonLeeeaf <150461955+MoonLeeeaf@users.noreply.github.com> Date: Wed, 10 Jul 2024 23:40:50 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E9=87=8D=E6=9E=84,=20=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=87=BD=E6=95=B0=E8=A3=85=E5=BA=93,=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=90=AF=E5=8A=A8=E6=96=87=E6=9C=AC=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=8F=8D=E5=BC=95=E5=8F=B7=E6=9D=A5=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=A1=8C=20=E5=B0=86log=E5=87=BD=E6=95=B0=E5=88=86=E5=87=BA?= =?UTF-8?q?=E5=88=B0=20log.js=20=E4=B8=AD=20=E9=87=8D=E6=96=B0=E7=BC=96?= =?UTF-8?q?=E5=86=99=E4=BA=86=E5=90=AF=E5=8A=A8=E7=9A=84=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E6=96=87=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server_src/log.js | 19 +++++++++++++++++++ server_src/main.js | 36 ++++++++++++++---------------------- server_src/ws-api.js | 6 ++---- 3 files changed, 35 insertions(+), 26 deletions(-) create mode 100644 server_src/log.js diff --git a/server_src/log.js b/server_src/log.js new file mode 100644 index 0000000..6cf4040 --- /dev/null +++ b/server_src/log.js @@ -0,0 +1,19 @@ +const color = require("./color") + +const log = (t) => { + console.log("[" + new Date().toLocaleTimeString('en-US', { hour12: false }) + "] " + t) +} + +const loge = (t) => { + log(`[E] ${color.red + t + color.none}`) +} + +const logw = (t) => { + log(`[W] ${color.yellow + t + color.none}`) +} + +module.exports = { + log: log, + loge: loge, + logw, logw, +} diff --git a/server_src/main.js b/server_src/main.js index 6b0b515..7f89a22 100644 --- a/server_src/main.js +++ b/server_src/main.js @@ -6,9 +6,7 @@ console.log("正在初始化...") -const log = (t) => { - console.log("[" + new Date().toLocaleTimeString('en-US', { hour12: false }) + "] " + t) -} +const { log, loge, logw } = require("./log") const sio = require("socket.io") const http = require("http") @@ -18,16 +16,6 @@ const process = require("process") const vals = require("./val") const color = require("./color") -// https://tool.lu/asciipainting/index.html?q=Geometric%20figures&type=0&page=2 -const banner = `${color.blue} - .+------+ - .' | .'| ${color.green}为人民服务————${color.blue} -+---+--+' | ${color.red}铃之椅 - Node.js${color.blue} -| .+--+---+ ${color.yellow}GitHub @MoonLeeeaf${color.blue} -|.' | .' -+------+'${color.none} -` - //定义 Http 服务器回调 let httpServerCallback = require("./http-api") @@ -59,19 +47,22 @@ let checkEmpty = (i) => { wsServer.on("connect", (client) => { - log("客户端 " + client.handshake.address + " 已连接, 用户名(未经验证): " + client.handshake.auth.name) + logw(`客户端 ${client.handshake.address} 已连接, 用户名(未经验证): ${client.handshake.auth.name}`) for (const cb in wsServerCallback) { client.on(cb, (...args) => { - log("客户端 " + client.handshake.address + " 对接口 [" + cb + "] 发起请求,参数为 " + JSON.stringify(args[0])) + log(`客户端 ${client.handshake.address} 对接口 [${cb}] 发起请求,参数为 ${JSON.stringify(args[0])}`) let callback = args[args.length - 1] try { wsServerCallback[cb](args[0], (reArgs) => { callback(reArgs) - log("返回接口 [" + cb + "] 到 " + client.handshake.address + ",参数为 " + JSON.stringify(reArgs)) + if (reArgs.code != 0) + logw(`返回接口 [${cb}] 到 ${client.handshake.address},参数为 ${JSON.stringify(reArgs)}`) + else + log(`返回接口 [${cb}] 到 ${client.handshake.address},参数为 ${JSON.stringify(reArgs)}`) }, client, cachedClients) } catch (e) { - log(color.yellow + "调用接口或返回数据时出错: " + e + color.none) + loge(`调用接口或返回数据时出错: ${e}`) callback({ code: -1, msg: e }) } }) @@ -79,7 +70,7 @@ wsServer.on("connect", (client) => { client.on("disconnect", () => { if (!client.handshake.auth.passCheck) - return log("未验证的客户端 " + client.handshake.address + " 已断开, 未验证的用户名: " + client.handshake.auth.name) + return logw(`未验证的客户端 ${client.handshake.address} 已断开, 未验证的用户名: ${client.handshake.auth.name}`) // 为了支持多客户端登录 我豁出去了 if (cachedClients[client.handshake.auth.name].length === 1) @@ -90,13 +81,14 @@ wsServer.on("connect", (client) => { arr.splice(index, 1) } }) - log("客户端 " + client.handshake.address + " 已断开, 用户名: " + client.handshake.auth.name) + log(`客户端 ${client.handshake.address} 已断开, 用户名: ${client.handshake.auth.name}`) }) }) httpServer.listen(vals.LINGCHAIR_SERVER_CONFIG.port) -console.log(banner) -log(color.green + "运行服务于端口 " + vals.LINGCHAIR_SERVER_CONFIG.port + " 上," + (vals.LINGCHAIR_SERVER_CONFIG.useHttps == true ? "已" : "未") + "使用 HTTPS" + color.none) -log(color.green + "服务已启动..." + color.none) +log(`${color.green}HTTP 和 WebSocket 服务已在端口 ${vals.LINGCHAIR_SERVER_CONFIG.port} 上启动,${vals.LINGCHAIR_SERVER_CONFIG.useHttps == true ? "已" : "未"}使用 HTTPS${color.none}`) +log(`${color.green}感谢使用!${color.none}`) +log(`${color.green}GitHub @MoonLeeeaf${color.none}`) +log(`${color.green}服务已启动...${color.none}`) diff --git a/server_src/ws-api.js b/server_src/ws-api.js index 294cfd3..220472b 100644 --- a/server_src/ws-api.js +++ b/server_src/ws-api.js @@ -4,9 +4,7 @@ * 铃之椅 Node 服务端 */ -const log = (t) => { - console.log("[" + new Date().toLocaleTimeString('en-US', { hour12: false }) + "] " + t) -} +const { log, loge, logw } = require("./log") const msgs = require("./api-msgs") const users = require("./api-users") @@ -48,7 +46,7 @@ let api = { if (!users.checkRefreshToken(a.name, a.refreshToken)) return cb({ code: -1, msg: "刷新令牌错误", invalid: true }) - log(color.yellow + "客户端 " + client.handshake.address + " 完成了用户 " + a.name + " 的验证" + color.none) + logw(`客户端 ${client.handshake.address} 完成了用户 ${a.name} 的验证`) // 更新映射 client.handshake.auth.passCheck = true