Skip to content

Commit 7e2f3c3

Browse files
committed
Prevent leave website when broadcasting
1 parent 2b60d59 commit 7e2f3c3

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

apps/web/pages/app/[locale]/tenants/[slug]/[id]/broadcast-via-browser.page.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { useRouter } from 'next/router';
5858
import { useWakeLock } from '~/utils/hooks/use-wake-lock';
5959
import { TimeCounter } from '~/atoms/time-counter';
6060
import { useConvertLiveId } from '~/utils/hooks/api/use-convert-live-id';
61+
import { useBeforeUnload } from 'react-use';
6162

6263
type Params = { slug: string; id: string };
6364

@@ -68,6 +69,7 @@ const Page: NextPage<PageProps<Props, Params & PathProps>> = ({
6869
const toast = useToast();
6970
const router = useRouter();
7071
const isInitializedRef = useRef(false);
72+
const liveEndedRef = useRef(false);
7173
const commentsRef = useRef<HTMLDivElement>(null);
7274
const [liveId] = useConvertLiveId(slug, id);
7375
const [stream, mutate] = useStream(liveId);
@@ -104,6 +106,13 @@ const Page: NextPage<PageProps<Props, Params & PathProps>> = ({
104106
onOpen: onOpenLiveEdit,
105107
onClose: onCloseLiveEdit
106108
} = useDisclosure();
109+
useBeforeUnload(
110+
useCallback(
111+
() => !!(live?.isPushing && !live?.endedAt && !liveEndedRef.current),
112+
[live]
113+
),
114+
'ページを離れると、配信が切断されます'
115+
);
107116

108117
const {
109118
isWakeLockSupported,
@@ -129,6 +138,7 @@ const Page: NextPage<PageProps<Props, Params & PathProps>> = ({
129138
});
130139

131140
if (!isStart) {
141+
liveEndedRef.current = true;
132142
void router.push('/');
133143
}
134144
} catch (e) {

apps/web/utils/hooks/api/use-push-via-browser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ export const usePushViaBrowser = (liveId?: number) => {
121121
void (async () => {
122122
try {
123123
const media = await navigator.mediaDevices.getUserMedia({
124-
audio: true
124+
audio: {
125+
echoCancellation: false,
126+
noiseSuppression: false,
127+
autoGainControl: false
128+
}
125129
});
126130

127131
audioStream = new MediaStream();

0 commit comments

Comments
 (0)