Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

마이크 없이 입장 이슈 해결 & 일부 코드 리팩토링 #150

Merged
merged 3 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/components/common/toast/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const NotificationContainer = styled.div`

font-size: 14px;

z-index: 10;
z-index: 1000;
`;

export const Notification = styled.div`
Expand Down
5 changes: 0 additions & 5 deletions client/src/components/room/new-view/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ function RoomModal() {
else if (isAnonymous) setRoomView('selectModeView');
else {
setRoomView('inRoomView');
setToastList({
type: 'success',
title: '방 생성',
description: '성공적으로 방이 생성됐습니다!',
});
}
})
.catch((err) => {
Expand Down
47 changes: 32 additions & 15 deletions client/src/hooks/useRtc.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {
MutableRefObject, RefObject, useCallback, useRef, useEffect, useState, Dispatch,
} from 'react';
import { useRecoilState, useRecoilValue } from 'recoil';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
import { Socket } from 'socket.io-client';

import useSocket from '@src/hooks/useSocket';
import { bindTrailingArgs } from '@src/utils';
import roomDocumentIdState from '@atoms/room-document-id';
import userTypeState, { IUser } from '@atoms/user';
import anonymousState from '@atoms/anonymous';
import roomViewState from '@atoms/room-view-type';
import toastListSelector from '@selectors/toast-list';

export interface IRTC {
socketId?: string,
Expand All @@ -24,16 +26,12 @@ export const useLocalStream = (): [MutableRefObject<MediaStream | null>, RefObje
const myVideoRef = useRef<HTMLVideoElement | null>(null);

const getLocalStream = useCallback(async () => {
try {
myStreamRef.current = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
if (myVideoRef.current) myVideoRef.current.srcObject = myStreamRef.current;
myStreamRef.current = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
if (myVideoRef.current) myVideoRef.current.srcObject = myStreamRef.current;
myStreamRef.current!
.getAudioTracks()
// eslint-disable-next-line
.forEach((track: MediaStreamTrack) => (track.enabled = !track.enabled));
} catch (e) {
console.error(e);
}
}, []);

return [myStreamRef, myVideoRef, getLocalStream];
Expand Down Expand Up @@ -116,14 +114,33 @@ export const useRtc = <T extends IRTC>(): [
const socket = useSocket('/room');
const [myStreamRef, myVideoRef, getLocalStream] = useLocalStream();
const setPeerConnection = useSetPeerConnection(setParticipants, myStreamRef);
const setRoomView = useSetRecoilState(roomViewState);
const setToastList = useSetRecoilState(toastListSelector);

useEffect(() => {
if (!socket) return;

const init = async () => {
await getLocalStream();
socket.emit('room:join', {
roomDocumentId, userDocumentId: user.userDocumentId, socketId: socket!.id, isAnonymous,
});
try {
await getLocalStream();
if (!myStreamRef.current) throw new Error('NOT_ALLOW_MIC');
socket.emit('room:join', {
roomDocumentId, userDocumentId: user.userDocumentId, socketId: socket!.id, isAnonymous,
});
setToastList({
type: 'success',
title: '방 생성',
description: '성공적으로 방이 생성됐습니다!',
});
} catch (error) {
console.error(error);
setToastList({
type: 'danger',
title: '장치 허용',
description: '마이크를 허용하지 않을 경우 방에 참가할 수 없습니다.',
});
setRoomView('createRoomView');
}
};

init();
Expand Down Expand Up @@ -188,10 +205,10 @@ export const useRtc = <T extends IRTC>(): [
return [];
});

myStreamRef.current!.getTracks()
.forEach((track: MediaStreamTrack) => {
track.stop();
});
myStreamRef.current?.getTracks()
.forEach((track: MediaStreamTrack) => {
track.stop();
});
};
}, [socket, setPeerConnection]);

Expand Down