1
1
mirror of https://github.com/ZeroCatDev/ClassworksKV.git synced 2025-12-07 21:13:10 +00:00

feat: 更新Socket.IO初始化配置,优化CORS设置和传输方式

This commit is contained in:
Sunwuyuan 2025-12-06 11:53:57 +08:00
parent e73ff53f58
commit 1e1b99a070
No known key found for this signature in database
GPG Key ID: A6A54CF66F56BB64

View File

@ -96,14 +96,16 @@ function detectDeviceName(userAgent, headers = {}) {
export function initSocket(server) {
if (io) return io;
const allowOrigin = process.env.FRONTEND_URL || "*";
io = new Server(server, {
cors: {
origin: allowOrigin,
origin: "*",
methods: ["GET", "POST"],
credentials: true,
allowedHeaders: ["*"],
credentials: false
},
// 传输方式回退策略优先使用WebSocket,回退到轮询
transports: ["polling", "websocket"],
});
io.on("connection", (socket) => {
@ -288,10 +290,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);
}
}