diff --git a/default.d.ts b/default.d.ts index 6b281da..78659ca 100644 --- a/default.d.ts +++ b/default.d.ts @@ -1,4 +1,5 @@ import { LoadingBarProviderInst, MessageProviderInst } from "naive-ui" +import { useRoute } from "vue-router" declare module 'vue3-aplayer' { const content: any @@ -17,5 +18,6 @@ declare global { interface Window { $message: MessageProviderInst, $loadingBar: LoadingBarProviderInst + $route: ReturnType } } \ No newline at end of file diff --git a/src/api/account.ts b/src/api/account.ts index f544c07..d5311fa 100644 --- a/src/api/account.ts +++ b/src/api/account.ts @@ -182,7 +182,19 @@ export function downloadConfigDirect(name: string) { name: name }) } -export async function DownloadConfig(name: string) { +export type ConfigStatus = 'success' | 'error' | 'notfound' +export async function DownloadConfig(name: string): Promise< + | { + msg: undefined + status: ConfigStatus + data: T + } + | { + msg: string + status: ConfigStatus + data: undefined + } +> { try { const resp = await QueryGetAPI(VTSURU_API_URL + 'get-config', { name: name @@ -191,18 +203,21 @@ export async function DownloadConfig(name: string) { console.log('已获取配置文件: ' + name) return { msg: undefined, + status: 'success', data: JSON.parse(resp.data) as T } } else if (resp.code == 404) { console.error(`未找到名为 ${name} 的配置文件`) return { msg: `未找到名为 ${name} 的配置文件, 需要先上传`, + status: 'notfound', data: undefined } } else { console.error(`无法获取配置文件 [${name}]: ` + resp.message) return { msg: `无法获取配置文件 [${name}]: ` + resp.message, + status: 'error', data: undefined } } @@ -210,6 +225,7 @@ export async function DownloadConfig(name: string) { console.error(`无法获取配置文件 [${name}]: ` + err) return { msg: `无法获取配置文件 [${name}]: ` + err, + status: 'error', data: undefined } } diff --git a/src/components/TempComponent.vue b/src/components/TempComponent.vue index 6664728..4a48ba9 100644 --- a/src/components/TempComponent.vue +++ b/src/components/TempComponent.vue @@ -1,19 +1,27 @@ diff --git a/src/data/RTCClient.ts b/src/data/RTCClient.ts index 7de9590..93571d5 100644 --- a/src/data/RTCClient.ts +++ b/src/data/RTCClient.ts @@ -99,11 +99,14 @@ abstract class BaseRTCClient { } public processData(conn: DataConnection, data: RTCData) { //console.log(data) - if (data.Key == 'Heartbeat') { // 心跳 + if (data.Key == 'Heartbeat') { + // 心跳 return - } else if (data.Key == 'VTsuru.RTCEvent.On') { // 添加事件 + } else if (data.Key == 'VTsuru.RTCEvent.On') { + // 添加事件 this.handledEvents[conn.peer].push(data.Data) - } else if (data.Key == 'VTsuru.RTCEvent.Off') { // 移除事件 + } else if (data.Key == 'VTsuru.RTCEvent.Off') { + // 移除事件 const i = this.handledEvents[conn.peer].indexOf(data.Data) if (i > -1) { this.handledEvents[conn.peer].splice(i, 1) @@ -127,12 +130,14 @@ abstract class BaseRTCClient { ) } - public Init() { + public async Init() { if (!this.isInited) { this.isInited = true + await this.vhub.on('RTCOffline', (id: string) => + this.onConnectionClose(id) + ) this.connectRTC() } - this.vhub.on('RTCOffline', (id: string) => this.onConnectionClose(id)) return this } } @@ -170,8 +175,8 @@ export class SlaveRTCClient extends BaseRTCClient { c?.on('data', (data) => this.processData(c, data as RTCData)) c?.on('close', () => this.onConnectionClose(c.peer)) } - public Init() { - super.Init() + public async Init() { + await super.Init() this.vhub?.on('MasterOnline', (data: string) => this.connectToMaster(data)) setTimeout(() => { this.connectToAllMaster() diff --git a/src/main.ts b/src/main.ts index 79dd434..5fdd3fa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -101,7 +101,6 @@ QueryGetAPI(BASE_API_URL + 'vtsuru/version') //加载其他数据 await GetSelfAccount() const account = useAccount() - const vhub = useVTsuruHub().Init(account.value.token) const useAuth = useAuthStore() if (account.value.id) { if (account.value.biliUserAuthInfo && !useAuth.currentToken) { diff --git a/src/store/useDanmakuClient.ts b/src/store/useDanmakuClient.ts index 3767f4c..4537e12 100644 --- a/src/store/useDanmakuClient.ts +++ b/src/store/useDanmakuClient.ts @@ -1,3 +1,4 @@ +import { useAccount } from '@/api/account' import DanmakuClient, { AuthInfo, RoomAuthInfo } from '@/data/DanmakuClient' import { defineStore } from 'pinia' import { computed, ref } from 'vue' @@ -18,6 +19,7 @@ export const useDanmakuClient = defineStore('DanmakuClient', () => { () => status.value === 'running' || status.value === 'listening' ) const authInfo = ref() + const accountInfo = useAccount() let existOtherClient = false let isInitializing = false @@ -75,7 +77,7 @@ export const useDanmakuClient = defineStore('DanmakuClient', () => { async (lock) => { if (lock) { status.value = 'initializing' - bc = new BroadcastChannel('vtsuru.danmaku') + bc = new BroadcastChannel('vtsuru.danmaku.' + accountInfo.value?.id) bc.onmessage = (event) => { const message: BCMessage = event.data as BCMessage const data = message.data ? JSON.parse(message.data) : {} @@ -83,7 +85,7 @@ export const useDanmakuClient = defineStore('DanmakuClient', () => { case 'check-client': sendBCMessage('response-client-status', { status: status.value, - auth: authInfo.value + auth: authInfo.value, }) break case 'response-client-status': diff --git a/src/store/useRTC.ts b/src/store/useRTC.ts index ae49664..c87f61e 100644 --- a/src/store/useRTC.ts +++ b/src/store/useRTC.ts @@ -9,7 +9,9 @@ export const useWebRTC = defineStore('WebRTC', () => { const accountInfo = useAccount() let isInitializing = false - function Init(type: 'master' | 'slave'): MasterRTCClient | SlaveRTCClient | undefined { + function Init( + type: 'master' | 'slave' + ): MasterRTCClient | SlaveRTCClient | undefined { if (isInitializing) { return } @@ -30,7 +32,7 @@ export const useWebRTC = defineStore('WebRTC', () => { accountInfo.value.id.toString(), accountInfo.value.token ) - masterClient.value.Init() + await masterClient.value.Init() return masterClient } } else { @@ -41,7 +43,7 @@ export const useWebRTC = defineStore('WebRTC', () => { accountInfo.value.id?.toString(), accountInfo.value.token ) - slaveClient.value.Init() + await slaveClient.value.Init() return slaveClient } } diff --git a/src/store/useVTsuruHub.ts b/src/store/useVTsuruHub.ts index 6a46e41..b244b14 100644 --- a/src/store/useVTsuruHub.ts +++ b/src/store/useVTsuruHub.ts @@ -13,14 +13,16 @@ export const useVTsuruHub = defineStore('VTsuruHub', () => { const signalRClient = ref() const isInited = ref(false) const isIniting = ref(false) - let token = '' + const accountInfo = useAccount() async function connectSignalR() { if (isIniting.value) return isIniting.value = true + while (!accountInfo.value.id || accountInfo.value.id < 1) + await new Promise((resolve) => setTimeout(resolve, 1000)) //console.log('[Components-Event] 正在连接到 VTsuru 服务器...') const connection = new HubConnectionBuilder() - .withUrl(BASE_HUB_URL + 'main?token=' + token, { + .withUrl(BASE_HUB_URL + 'main?token=' + accountInfo.value.token, { skipNegotiation: true, transport: HttpTransportType.WebSockets, logger: LogLevel.Error @@ -91,9 +93,8 @@ export const useVTsuruHub = defineStore('VTsuruHub', () => { signalRClient.value?.onreconnected(listener) } - function Init(_token: string) { - token = _token - if (!isInited.value) { + function Init() { + if (!isInited.value && !isIniting.value) { connectSignalR() } return useVTsuruHub() diff --git a/src/views/TestView.vue b/src/views/TestView.vue index 3c7e00d..15e0c15 100644 --- a/src/views/TestView.vue +++ b/src/views/TestView.vue @@ -39,6 +39,6 @@ function mount() { - + \ No newline at end of file diff --git a/src/views/obs/DanmujiOBS.vue b/src/views/obs/DanmujiOBS.vue index 94899a3..42828f8 100644 --- a/src/views/obs/DanmujiOBS.vue +++ b/src/views/obs/DanmujiOBS.vue @@ -17,6 +17,8 @@ import { useWebRTC } from '@/store/useRTC'; import { QueryGetAPI } from '@/api/query'; import { OPEN_LIVE_API_URL, VTSURU_API_URL } from '@/data/constants'; import { CustomChart } from 'echarts/charts'; +import { useRoute } from 'vue-router'; +import { NAlert } from 'naive-ui'; export interface DanmujiConfig { minGiftPrice: number, @@ -42,15 +44,16 @@ export interface DanmujiConfig { } defineExpose({ setCss }) -const props = defineProps<{ +const { customCss, isOBS = true } = defineProps<{ customCss?: string + isOBS?: boolean }>() const messageRender = ref() const client = await useDanmakuClient().initClient() const pronunciationConverter = new pronunciation.PronunciationConverter() const accountInfo = useAccount() - +const route = useRoute() const defaultConfig: DanmujiConfig = { minGiftPrice: 0.1, @@ -389,7 +392,7 @@ onMounted(async () => { config.value = result.data as DanmujiConfig break } - else { + else if (result.status == 'error') { await new Promise(resolve => setTimeout(resolve, 1000)) } } @@ -406,5 +409,7 @@ onUnmounted(() => { \ No newline at end of file