Skip to content

Commit

Permalink
Prevent leave website when broadcasting
Browse files Browse the repository at this point in the history
  • Loading branch information
nzws committed Oct 4, 2022
1 parent 2b60d59 commit 7e2f3c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { useRouter } from 'next/router';
import { useWakeLock } from '~/utils/hooks/use-wake-lock';
import { TimeCounter } from '~/atoms/time-counter';
import { useConvertLiveId } from '~/utils/hooks/api/use-convert-live-id';
import { useBeforeUnload } from 'react-use';

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

Expand All @@ -68,6 +69,7 @@ const Page: NextPage<PageProps<Props, Params & PathProps>> = ({
const toast = useToast();
const router = useRouter();
const isInitializedRef = useRef(false);
const liveEndedRef = useRef(false);
const commentsRef = useRef<HTMLDivElement>(null);
const [liveId] = useConvertLiveId(slug, id);
const [stream, mutate] = useStream(liveId);
Expand Down Expand Up @@ -104,6 +106,13 @@ const Page: NextPage<PageProps<Props, Params & PathProps>> = ({
onOpen: onOpenLiveEdit,
onClose: onCloseLiveEdit
} = useDisclosure();
useBeforeUnload(
useCallback(
() => !!(live?.isPushing && !live?.endedAt && !liveEndedRef.current),
[live]
),
'ページを離れると、配信が切断されます'
);

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

if (!isStart) {
liveEndedRef.current = true;
void router.push('/');
}
} catch (e) {
Expand Down
6 changes: 5 additions & 1 deletion apps/web/utils/hooks/api/use-push-via-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ export const usePushViaBrowser = (liveId?: number) => {
void (async () => {
try {
const media = await navigator.mediaDevices.getUserMedia({
audio: true
audio: {
echoCancellation: false,
noiseSuppression: false,
autoGainControl: false
}
});

audioStream = new MediaStream();
Expand Down

1 comment on commit 7e2f3c3

@vercel
Copy link

@vercel vercel bot commented on 7e2f3c3 Oct 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

knzklive2 – ./

knzklive2-git-production-knzklive.vercel.app
knzklive2-knzklive.vercel.app
knzk.live

Please sign in to comment.