From f635bb58cc2fcabea1a32f33e76011933e492a23 Mon Sep 17 00:00:00 2001 From: yj <1336058017@qq.com> Date: Tue, 23 Jul 2024 17:03:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=AD=E7=BD=91=E9=87=8D=E8=BF=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/page/Home/index.tsx | 2 +- src/page/Meeting/index.tsx | 24 ++++++++++++++++++++++-- src/utils/package/signalr.ts | 6 +++--- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/page/Home/index.tsx b/src/page/Home/index.tsx index b30805b..966ba23 100644 --- a/src/page/Home/index.tsx +++ b/src/page/Home/index.tsx @@ -51,7 +51,7 @@ const Home: React.FC = () => { useEffect(() => { const user = JSON.parse(storage.getItem('user') as string); setUserInfo(user) - startSignalr() + startSignalr(() => { }) const updateTime = () => { setDateInfo({ work: dayjs().format('ddd'), diff --git a/src/page/Meeting/index.tsx b/src/page/Meeting/index.tsx index ad1c212..5c0010e 100644 --- a/src/page/Meeting/index.tsx +++ b/src/page/Meeting/index.tsx @@ -12,7 +12,7 @@ import { GetRoomFile, PostRoomFile, DeleteRoomFile, GetRoomUpFileurl, GetRoomFil import axios from 'axios'; import ImageUrl from '@/utils/package/imageUrl' import agora from '@/utils/package/agora' -import { onInvoke, onSignalr, offSignalr } from '@/utils/package/signalr'; +import { onInvoke, onSignalr, offSignalr, startSignalr } from '@/utils/package/signalr'; import dayjs from 'dayjs'; import durationPlugin from 'dayjs/plugin/duration'; import { VideoSourceType } from 'agora-electron-sdk'; @@ -172,6 +172,8 @@ const Meeting: React.FC = () => { setUser(userInfo) setIsInit(false) window.addEventListener('customStorageChange', handleCustomStorageChange); + window.addEventListener('online', handleNetworkChange); + window.addEventListener('offline', handleNetworkChange); time = setInterval(() => { let effectiveTypeLength = ['slow-2g', '2g', '3g', '4g'].indexOf((navigator as any).connection.effectiveType) if (effectiveTypeLength >= 0) { @@ -184,6 +186,8 @@ const Meeting: React.FC = () => { } return () => { window.removeEventListener('customStorageChange', handleCustomStorageChange); + window.removeEventListener('online', handleNetworkChange); + window.removeEventListener('offline', handleNetworkChange); clearInterval(time) }; }, [fileList.pageIndex]); @@ -241,8 +245,24 @@ const Meeting: React.FC = () => { }, [currentVideoId]) + // 网络 + const handleNetworkChange = (): void => { + if (navigator.onLine) { + message.success('网络已恢复。') + startSignalr(async () => { + await onInvoke('joinChannel', { + roomNum: state.channelId, + enableMicr: true, + enableCamera: true + }) + await getRoomUser() + }) + } else { + message.error('网络已断开!') + } + } // 加入房间时间 - const changeCurrentSeconds = () => { + const changeCurrentSeconds = (): string => { const duration = dayjs.duration(currentSeconds, 'seconds'); const hours = duration.hours(); // 整数小时 const minutes = duration.minutes(); // 整数分钟 diff --git a/src/utils/package/signalr.ts b/src/utils/package/signalr.ts index 36b84e6..8adf3e7 100644 --- a/src/utils/package/signalr.ts +++ b/src/utils/package/signalr.ts @@ -1,7 +1,7 @@ import * as signalR from '@microsoft/signalr'; import storage from './storage'; let connection = null as any; -export const startSignalr = async () => { +export const startSignalr = async (callBack: Function) => { const user = await JSON.parse(storage.getItem('user') as string); connection = new signalR.HubConnectionBuilder().withAutomaticReconnect() .withUrl(`${import.meta.env.VITE_BASE_URL_API}/session-manage`, { @@ -12,8 +12,8 @@ export const startSignalr = async () => { .build(); connection.start() - .then(() => console.log('Connection started!')) - .catch((err: any) => setTimeout(()=> startSignalr(), 5000)); + .then(() => callBack()) + .catch((err: any) => setTimeout(() => startSignalr(() => { }), 5000)); } export const onInvitation = (callBack: Function) => {