mirror of
https://github.com/Megghy/vtsuru.live.git
synced 2025-12-08 11:26:56 +08:00
feat: 添加弹幕窗口管理功能;优化弹幕客户端连接逻辑;实现自动滚动和设置更新; 修复浏览页页面切换的问题
This commit is contained in:
@@ -20,7 +20,7 @@ type GenericListener = Listener | AllEventListener;
|
||||
|
||||
export const useDanmakuClient = defineStore('DanmakuClient', () => {
|
||||
// 使用 shallowRef 存储 danmakuClient 实例, 性能稍好
|
||||
const danmakuClient = shallowRef<BaseDanmakuClient>();
|
||||
const danmakuClient = shallowRef<BaseDanmakuClient | undefined>(new OpenLiveClient()); // 默认实例化一个 OpenLiveClient
|
||||
|
||||
// 连接状态: 'waiting'-等待初始化, 'connecting'-连接中, 'connected'-已连接
|
||||
const state = ref<'waiting' | 'connecting' | 'connected'>('waiting');
|
||||
@@ -43,7 +43,7 @@ export const useDanmakuClient = defineStore('DanmakuClient', () => {
|
||||
function onEvent(eventName: EventName, listener: Listener): void;
|
||||
// --- 修正点: 实现签名使用联合类型,并在内部进行类型断言 ---
|
||||
function onEvent(eventName: keyof BaseDanmakuClient['eventsAsModel'], listener: GenericListener): void {
|
||||
if(!danmakuClient.value) {
|
||||
if (!danmakuClient.value) {
|
||||
console.warn("[DanmakuClient] 尝试在客户端初始化之前调用 'onEvent'。");
|
||||
return;
|
||||
}
|
||||
@@ -189,7 +189,9 @@ export const useDanmakuClient = defineStore('DanmakuClient', () => {
|
||||
// 先停止并清理旧客户端 (如果存在)
|
||||
if (danmakuClient.value) {
|
||||
console.log('[DanmakuClient] 正在处理旧的客户端实例...');
|
||||
await disposeClientInstance(danmakuClient.value);
|
||||
if (danmakuClient.value.state === 'connected') {
|
||||
await disposeClientInstance(danmakuClient.value);
|
||||
}
|
||||
danmakuClient.value = undefined; // 显式清除旧实例引用
|
||||
}
|
||||
|
||||
|
||||
@@ -186,6 +186,7 @@ export const useWebFetcher = defineStore('WebFetcher', () => {
|
||||
return { success: false, message: '未提供弹幕客户端认证信息' };
|
||||
}
|
||||
await client.initDirect(directConnectInfo);
|
||||
return { success: true, message: '弹幕客户端已启动' };
|
||||
}
|
||||
|
||||
// 监听所有事件,用于处理和转发
|
||||
@@ -197,11 +198,14 @@ export const useWebFetcher = defineStore('WebFetcher', () => {
|
||||
danmakuServerUrl.value = client.danmakuClient!.serverUrl; // 获取服务器地址
|
||||
// 启动事件发送定时器 (如果之前没有启动)
|
||||
timer ??= setInterval(sendEvents, 1500); // 每 1.5 秒尝试发送一次事件
|
||||
return { success: true, message: '弹幕客户端已启动' };
|
||||
} else {
|
||||
console.error(prefix.value + '弹幕客户端启动失败');
|
||||
danmakuClientState.value = 'stopped';
|
||||
danmakuServerUrl.value = undefined;
|
||||
client.dispose(); // 启动失败,清理实例,下次会重建
|
||||
return { success: false, message: '弹幕客户端启动失败' };
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,11 +269,11 @@ export const useWebFetcher = defineStore('WebFetcher', () => {
|
||||
// --- 尝试启动连接 ---
|
||||
try {
|
||||
await connection.start();
|
||||
console.log(prefix.value + '已连接到 vtsuru 服务器, ConnectionId: ' + connection.connectionId); // 调试输出连接状态
|
||||
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;
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user