更新依赖项,优化 WebSocket 处理,添加系统版本路由
This commit is contained in:
@@ -5,7 +5,7 @@ import { getLoginUser } from '@/modules/auth.ts';
|
||||
import { logger } from '../logger.ts';
|
||||
export const wsProxyManager = new WsProxyManager();
|
||||
|
||||
export const upgrade = async (request: any, socket: any, head: any) => {
|
||||
export const upgrade = (request: any, socket: any, head: any) => {
|
||||
const req = request as any;
|
||||
const url = new URL(req.url, 'http://localhost');
|
||||
const id = url.searchParams.get('id');
|
||||
@@ -13,6 +13,7 @@ export const upgrade = async (request: any, socket: any, head: any) => {
|
||||
console.log('upgrade', request.url, id);
|
||||
wss.handleUpgrade(req, socket, head, (ws) => {
|
||||
// 这里手动触发 connection 事件
|
||||
console.log('emitting connection event');
|
||||
// @ts-ignore
|
||||
wss.emit('connection', ws, req);
|
||||
});
|
||||
@@ -28,15 +29,21 @@ export const wss = new WebSocketServer({
|
||||
wss.on('connection', async (ws, req) => {
|
||||
console.log('connected', req.url);
|
||||
const url = new URL(req.url, 'http://localhost');
|
||||
const id = url?.searchParams?.get('id') || nanoid();
|
||||
const _id = url?.searchParams?.get('id');
|
||||
const id = _id || nanoid();
|
||||
const loginUser = await getLoginUser(req);
|
||||
if (!loginUser) {
|
||||
ws.send(JSON.stringify({ code: 401, message: 'No Login' }));
|
||||
ws.close();
|
||||
console.log('未登录,断开连接');
|
||||
ws.send(JSON.stringify({ code: 401, message: '未登录' }));
|
||||
setTimeout(() => {
|
||||
ws.close();
|
||||
}, 1000);
|
||||
return;
|
||||
}
|
||||
const user = loginUser.tokenUser?.username;
|
||||
wsProxyManager.register(id, { user, ws });
|
||||
const user = loginUser.tokenUser.username;
|
||||
const userApp = user + '-' + id;
|
||||
console.log('注册 ws 连接', userApp);
|
||||
wsProxyManager.register(userApp, { user, ws });
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'connected',
|
||||
@@ -54,16 +61,103 @@ wss.on('connection', async (ws, req) => {
|
||||
});
|
||||
ws.on('close', () => {
|
||||
logger.debug('ws closed');
|
||||
wsProxyManager.unregister(id, user);
|
||||
wsProxyManager.unregister(userApp);
|
||||
});
|
||||
});
|
||||
|
||||
export class WssApp {
|
||||
wss: WebSocketServer;
|
||||
bunWSS = websocket;
|
||||
constructor() {
|
||||
this.wss = wss;
|
||||
}
|
||||
upgrade(request: any, socket: any, head: any) {
|
||||
return upgrade(request, socket, head);
|
||||
// return upgrade(request, socket, head);
|
||||
return bunUpgrade(request);
|
||||
}
|
||||
}
|
||||
|
||||
export const bunUpgrade = (request: Request) => {
|
||||
const url = new URL(request.url, 'http://localhost');
|
||||
const isUpgrade = url.pathname === '/ws/proxy';
|
||||
if (isUpgrade) {
|
||||
console.log('upgrade', request.url);
|
||||
|
||||
// 使用 Bun 原生 WebSocket
|
||||
new Response(null, {
|
||||
status: 101,
|
||||
headers: {
|
||||
'Upgrade': 'websocket',
|
||||
},
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
// Bun WebSocket 处理器
|
||||
export const websocket = {
|
||||
async open(ws: any) {
|
||||
console.log('WebSocket opened');
|
||||
const { url, token } = ws.data;
|
||||
|
||||
const urlObj = new URL(url, 'http://localhost');
|
||||
const _id = urlObj.searchParams.get('id');
|
||||
const id = _id || nanoid();
|
||||
|
||||
// 创建一个模拟的 request 对象用于认证
|
||||
const mockReq: any = {
|
||||
url: url,
|
||||
headers: {
|
||||
authorization: token ? `Bearer ${token}` : undefined,
|
||||
},
|
||||
};
|
||||
|
||||
const loginUser = await getLoginUser(mockReq);
|
||||
|
||||
if (!loginUser) {
|
||||
console.log('未登录,断开连接');
|
||||
ws.send(JSON.stringify({ code: 401, message: '未登录' }));
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
|
||||
const user = loginUser.tokenUser.username;
|
||||
const userApp = user + '-' + id;
|
||||
console.log('注册 ws 连接', userApp);
|
||||
|
||||
ws.data.userApp = userApp;
|
||||
ws.data.user = user;
|
||||
|
||||
wsProxyManager.register(userApp, { user, ws });
|
||||
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'connected',
|
||||
user: user,
|
||||
id,
|
||||
}),
|
||||
);
|
||||
},
|
||||
|
||||
async message(ws: any, message: string) {
|
||||
try {
|
||||
const data = JSON.parse(message);
|
||||
logger.debug('message', data);
|
||||
} catch (error) {
|
||||
logger.error('Failed to parse message', error);
|
||||
}
|
||||
},
|
||||
|
||||
close(ws: any) {
|
||||
const { userApp } = ws.data;
|
||||
logger.debug('ws closed', userApp);
|
||||
if (userApp) {
|
||||
wsProxyManager.unregister(userApp);
|
||||
}
|
||||
},
|
||||
|
||||
error(ws: any, error: Error) {
|
||||
console.error('WebSocket error:', error);
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user