121 lines
4.3 KiB
JavaScript
121 lines
4.3 KiB
JavaScript
import express from "express";
|
|
import http from "http";
|
|
import log4js from "log4js";
|
|
import apiRouter from "./router/api.js"
|
|
import { Server } from "socket.io";
|
|
import { saveToDatabase } from "./utils/storage.js";
|
|
import { onlineList, clientMap } from "./data.js";
|
|
import { verifyToken } from "./utils/auth.js";
|
|
|
|
const PORT = process.env.PORT || 1111;
|
|
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
const io = new Server(server, {
|
|
cors: {
|
|
origin: "*",
|
|
methods: ["GET", "POST"],
|
|
},
|
|
});
|
|
|
|
const logger = log4js.getLogger("app");
|
|
|
|
app.use(express.static("static"))
|
|
app.use(apiRouter)
|
|
logger.level = process.env.LOG_LEVEL || "debug";
|
|
|
|
// 初始化在线列表
|
|
// let onlineList = [];
|
|
// let clientMap = new Map();
|
|
|
|
class Response {
|
|
constructor(status, msg) {
|
|
this.status = status
|
|
this.msg = msg
|
|
}
|
|
}
|
|
|
|
|
|
|
|
io.on("connection", (socket) => {
|
|
logger.info(`A client connected, id: ${socket.id}`)
|
|
socket.on("system", data => {
|
|
if (!data) {
|
|
socket.emit("system", JSON.stringify(new Response(false, "data payload is empty.")))
|
|
return;
|
|
}
|
|
if (clientMap.has(socket.id)) {
|
|
socket.emit("system", JSON.stringify(new Response(false, "you are already online.")))
|
|
return;
|
|
}
|
|
try {
|
|
const { opt, args } = JSON.parse(data);
|
|
logger.trace(opt, args)
|
|
if (opt == "signin") {
|
|
if (!args || !args.token) {
|
|
socket.emit("system", JSON.stringify(new Response(false, "token is empty.")))
|
|
return;
|
|
}
|
|
const { token } = args
|
|
// const username = token.trim();
|
|
verifyToken(token)
|
|
.then(res => {
|
|
if (res.data.code != 200) {
|
|
socket.emit("system", JSON.stringify(new Response(false, "token is invaild.")))
|
|
username = res.data.UserName;
|
|
uid = res.data.ID;
|
|
return;
|
|
}
|
|
if (onlineList.indexOf(username) != -1) {
|
|
socket.emit("system", JSON.stringify(new Response(false, "token is already taken.")))
|
|
return;
|
|
}
|
|
socket.emit("system", JSON.stringify(new Response(true, "sign in successfully.")))
|
|
socket.emit("username", username)
|
|
socket.username = username;
|
|
onlineList.push(username);
|
|
clientMap.set(socket.id, username);
|
|
io.emit("notice", JSON.stringify(new Response(true, `${socket.username} join the chat.`)))
|
|
logger.debug(`socket(${socket.id}) -- ${username} signed in.`)
|
|
})
|
|
.catch(err => {
|
|
logger.error(`socket(${socket.id}) -- ${err}.`)
|
|
socket.emit("system", JSON.stringify(new Response(false, "token is invaild.")))
|
|
return;
|
|
})
|
|
}
|
|
|
|
} catch (err) {
|
|
logger.error(`socket(${socket.id}) -- ${err}.`)
|
|
socket.emit("system", JSON.stringify(new Response(false, "data payload is invaild.")))
|
|
return;
|
|
}
|
|
logger.trace(clientMap)
|
|
logger.trace(onlineList)
|
|
})
|
|
|
|
socket.on("msg", data => {
|
|
if (!socket.username) {
|
|
socket.emit("system", JSON.stringify(new Response(false, "you are not signed in.")));
|
|
return;
|
|
}
|
|
logger.debug(`socket(${socket.id}) -- ${data}`)
|
|
io.emit("msg", JSON.stringify({sender: socket.username, msg: data}))
|
|
saveToDatabase(socket.username, data, new Date().getTime())
|
|
})
|
|
|
|
socket.on("disconnect", (reason, description) => {
|
|
logger.info(`A client disconnected, id: ${socket.id}`)
|
|
logger.trace(`socket(${socket.id}) -- reason: ${reason}, description: ${description}`);
|
|
if (clientMap.has(socket.id)) clientMap.delete(socket.id);
|
|
// TODO: 这个算法有点不安全
|
|
if (onlineList.includes(socket.username)) onlineList.splice(onlineList.indexOf(socket.id));
|
|
io.emit("notice", JSON.stringify(new Response(true, `${socket.username} left the chat.`)));
|
|
})
|
|
});
|
|
|
|
server.listen(PORT, () => {
|
|
logger.info(`Server running on port ${PORT}`);
|
|
});
|