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