mirror of
https://github.com/ZeroCatDev/ClassworksKV.git
synced 2025-12-10 08:03:09 +00:00
Compare commits
No commits in common. "e65f84aa22b5960391c5649d57c8ff41564ac106" and "63716e04290a63ab170c6ba47cf944743b94f1f3" have entirely different histories.
e65f84aa22
...
63716e0429
4
app.js
4
app.js
@ -25,10 +25,6 @@ app.use(
|
||||
cors({
|
||||
exposedHeaders: ["ratelimit-policy", "retry-after", "ratelimit"], // 告诉浏览器这些响应头可以暴露
|
||||
maxAge: 86400, // 设置OPTIONS请求的结果缓存24小时(86400秒),减少预检请求
|
||||
credentials: true, // 允许跨域请求携带凭证
|
||||
allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With", "Accept"], // 允许的请求头
|
||||
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], // 允许的HTTP方法
|
||||
withCredentials: true, // 允许携带cookie等凭证信息
|
||||
})
|
||||
);
|
||||
app.disable("x-powered-by");
|
||||
|
||||
@ -354,21 +354,9 @@ router.post(
|
||||
|
||||
const deviceId = res.locals.deviceId;
|
||||
const {key} = req.params;
|
||||
let value = req.body;
|
||||
const value = req.body;
|
||||
|
||||
// 处理空值,转换为空对象
|
||||
if (value === null || value === undefined || value === '') {
|
||||
value = {};
|
||||
}
|
||||
|
||||
// 验证是否能被 JSON 序列化
|
||||
try {
|
||||
JSON.stringify(value);
|
||||
} catch (error) {
|
||||
return next(
|
||||
errors.createError(400, "无效的数据格式")
|
||||
);
|
||||
}
|
||||
|
||||
// 获取客户端IP
|
||||
const creatorIp =
|
||||
|
||||
@ -96,16 +96,14 @@ function detectDeviceName(userAgent, headers = {}) {
|
||||
export function initSocket(server) {
|
||||
if (io) return io;
|
||||
|
||||
const allowOrigin = process.env.FRONTEND_URL || "*";
|
||||
|
||||
io = new Server(server, {
|
||||
cors: {
|
||||
origin: "*",
|
||||
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
|
||||
allowedHeaders: ["*"],
|
||||
credentials: false
|
||||
origin: allowOrigin,
|
||||
methods: ["GET", "POST"],
|
||||
credentials: true,
|
||||
},
|
||||
// 传输方式回退策略:优先使用WebSocket,回退到轮询
|
||||
transports: ["polling", "websocket"],
|
||||
});
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
@ -290,10 +288,10 @@ export function initSocket(server) {
|
||||
|
||||
// 清理socket相关缓存
|
||||
if (socket.data.currentToken) {
|
||||
// 如果这是该token的最后一个连接,考虑清理缓存
|
||||
// 如果这是该token的最后一个连接,考虑清理缓存
|
||||
const tokenSet = onlineTokens.get(socket.data.currentToken);
|
||||
if (!tokenSet || tokenSet.size === 0) {
|
||||
// 可以选择保留缓存一段时间,这里暂时保留
|
||||
// 可以选择保留缓存一段时间,这里暂时保留
|
||||
// tokenInfoCache.delete(socket.data.currentToken);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user