mirror of
https://github.com/Megghy/vtsuru.live.git
synced 2025-12-06 18:36:55 +08:00
并发控制
This commit is contained in:
@@ -211,7 +211,8 @@ export const useWebFetcher = defineStore('WebFetcher', () => {
|
||||
/**
|
||||
* 连接 SignalR 服务器
|
||||
*/
|
||||
async function connectSignalR() {
|
||||
async function connectSignalR(): Promise<boolean> {
|
||||
return await navigator.locks.request('webFetcherStartLock', async () => {
|
||||
if (signalRClient.value && signalRClient.value.state !== signalR.HubConnectionState.Disconnected) {
|
||||
console.log(prefix.value + "SignalR 已连接或正在连接");
|
||||
return true;
|
||||
@@ -234,6 +235,19 @@ export const useWebFetcher = defineStore('WebFetcher', () => {
|
||||
.withHubProtocol(new msgpack.MessagePackHubProtocol()) // 使用 MessagePack 协议
|
||||
.build();
|
||||
|
||||
connection.on('Disconnect', (reason: unknown) => {
|
||||
console.log(prefix.value + '被服务器断开连接: ' + reason);
|
||||
disconnectedByServer = true; // 标记是服务器主动断开
|
||||
window.$message.error(`被服务器要求断开连接: ${reason}`);
|
||||
});
|
||||
// --- 尝试启动连接 ---
|
||||
try {
|
||||
await connection.start();
|
||||
signalRConnectionId.value = connection.connectionId ?? undefined; // 保存连接ID
|
||||
signalRId.value = await sendSelfInfo(connection); // 发送客户端信息
|
||||
await connection.send('Finished'); // 通知服务器已准备好
|
||||
signalRClient.value = connection; // 保存实例
|
||||
|
||||
// --- SignalR 事件监听 ---
|
||||
connection.onreconnecting(error => {
|
||||
console.log(prefix.value + `与服务器断开,正在尝试重连... ${error?.message || ''}`);
|
||||
@@ -271,21 +285,9 @@ export const useWebFetcher = defineStore('WebFetcher', () => {
|
||||
console.log(prefix.value + `连接已手动关闭.`);
|
||||
}
|
||||
});
|
||||
|
||||
connection.on('Disconnect', (reason: unknown) => {
|
||||
console.log(prefix.value + '被服务器断开连接: ' + reason);
|
||||
disconnectedByServer = true; // 标记是服务器主动断开
|
||||
window.$message.error(`被服务器要求断开连接: ${reason}`);
|
||||
});
|
||||
connection.on('Request', async (url: string, method: string, body: string, useCookie: boolean) => onRequest(url, method, body, useCookie));
|
||||
connection.on('Notification', (type: string, data: any) => { onReceivedNotification(type, data); });
|
||||
// --- 尝试启动连接 ---
|
||||
try {
|
||||
await connection.start();
|
||||
signalRConnectionId.value = connection.connectionId ?? undefined; // 保存连接ID
|
||||
signalRId.value = await sendSelfInfo(connection); // 发送客户端信息
|
||||
await connection.send('Finished'); // 通知服务器已准备好
|
||||
signalRClient.value = connection; // 保存实例
|
||||
|
||||
console.log(prefix.value + '已连接到 vtsuru 服务器, ConnectionId: ' + signalRId.value); // 调试输出连接状态
|
||||
// state.value = 'connected'; // 状态将在 Start 函数末尾统一设置
|
||||
return true;
|
||||
@@ -296,6 +298,7 @@ export const useWebFetcher = defineStore('WebFetcher', () => {
|
||||
// state.value = 'disconnected'; // 保持 connecting 或由 Start 控制
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
async function sendSelfInfo(client: signalR.HubConnection) {
|
||||
return client.invoke('SetSelfInfo',
|
||||
|
||||
Reference in New Issue
Block a user