chore: 重构, 日志函数装库, 修改启动文本

使用反引号来重构字符串相关代码行
将log函数分出到 log.js 中
重新编写了启动的提示文本
This commit is contained in:
MoonLeeeaf
2024-07-10 23:40:50 +08:00
parent afcec52b79
commit 2bde59e5cc
3 changed files with 35 additions and 26 deletions

19
server_src/log.js Normal file
View File

@@ -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,
}

View File

@@ -6,9 +6,7 @@
console.log("正在初始化...") console.log("正在初始化...")
const log = (t) => { const { log, loge, logw } = require("./log")
console.log("[" + new Date().toLocaleTimeString('en-US', { hour12: false }) + "] " + t)
}
const sio = require("socket.io") const sio = require("socket.io")
const http = require("http") const http = require("http")
@@ -18,16 +16,6 @@ const process = require("process")
const vals = require("./val") const vals = require("./val")
const color = require("./color") 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 服务器回调 //定义 Http 服务器回调
let httpServerCallback = require("./http-api") let httpServerCallback = require("./http-api")
@@ -59,19 +47,22 @@ let checkEmpty = (i) => {
wsServer.on("connect", (client) => { 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) { for (const cb in wsServerCallback) {
client.on(cb, (...args) => { 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] let callback = args[args.length - 1]
try { try {
wsServerCallback[cb](args[0], (reArgs) => { wsServerCallback[cb](args[0], (reArgs) => {
callback(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) }, client, cachedClients)
} catch (e) { } catch (e) {
log(color.yellow + "调用接口或返回数据时出错: " + e + color.none) loge(`调用接口或返回数据时出错: ${e}`)
callback({ code: -1, msg: e }) callback({ code: -1, msg: e })
} }
}) })
@@ -79,7 +70,7 @@ wsServer.on("connect", (client) => {
client.on("disconnect", () => { client.on("disconnect", () => {
if (!client.handshake.auth.passCheck) 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) if (cachedClients[client.handshake.auth.name].length === 1)
@@ -90,13 +81,14 @@ wsServer.on("connect", (client) => {
arr.splice(index, 1) 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) httpServer.listen(vals.LINGCHAIR_SERVER_CONFIG.port)
console.log(banner) log(`${color.green}HTTP 和 WebSocket 服务已在端口 ${vals.LINGCHAIR_SERVER_CONFIG.port} 上启动,${vals.LINGCHAIR_SERVER_CONFIG.useHttps == true ? "已" : "未"}使用 HTTPS${color.none}`)
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 + "服务已启动..." + color.none) log(`${color.green}GitHub @MoonLeeeaf${color.none}`)
log(`${color.green}服务已启动...${color.none}`)

View File

@@ -4,9 +4,7 @@
* 铃之椅 Node 服务端 * 铃之椅 Node 服务端
*/ */
const log = (t) => { const { log, loge, logw } = require("./log")
console.log("[" + new Date().toLocaleTimeString('en-US', { hour12: false }) + "] " + t)
}
const msgs = require("./api-msgs") const msgs = require("./api-msgs")
const users = require("./api-users") const users = require("./api-users")
@@ -48,7 +46,7 @@ let api = {
if (!users.checkRefreshToken(a.name, a.refreshToken)) if (!users.checkRefreshToken(a.name, a.refreshToken))
return cb({ code: -1, msg: "刷新令牌错误", invalid: true }) 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 client.handshake.auth.passCheck = true