Skip to content

Commit 05d5605

Browse files
authored
Merge pull request #91 from boostcampwm-2022/88-socket-webrtc-실제-테스트-후-리팩토링
Socket webrtc 실제 테스트 후 리팩토링
2 parents 8c4c5c2 + 5f7e26c commit 05d5605

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

backend/socket/src/room/room.gateway.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export class RoomGateway implements OnGatewayConnection, OnGatewayDisconnect {
5353

5454
handleDisconnect(@ConnectedSocket() client: Socket) {
5555
this.logger.log(`disconnected: ${client.id}`);
56-
this.connectionService.leaveRoom({ client, server: this.server });
5756
this.webrtcService.disconnectWebrtc({ client, server: this.server });
57+
this.connectionService.leaveRoom({ client, server: this.server });
5858
}
5959

6060
// interview

backend/socket/src/room/room.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { InmemoryRoomRepository } from './repository/inmemory-room.repository';
44
import { RoomGateway } from './room.gateway';
55
import { ConnectionService } from './service/connection/connection.service';
66
import { InterviewService } from './service/interview/interview.service';
7+
import { WebrtcService } from './service/webRTC/webrtc.service';
78

89
export const RoomRepository: ClassProvider = {
910
provide: ROOM_REPOSITORY_INTERFACE,
1011
useClass: InmemoryRoomRepository,
1112
};
1213

1314
@Module({
14-
providers: [RoomGateway, RoomRepository, ConnectionService, InterviewService],
15+
providers: [RoomGateway, RoomRepository, ConnectionService, InterviewService, WebrtcService],
1516
})
1617
export class RoomModule {}

backend/socket/src/room/service/webRTC/webrtc.service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export class WebrtcService {
1313

1414
startSignaling({ client, server }: { client: Socket; server: Namespace }) {
1515
const user = this.roomRepository.getUserByClientId(client.id);
16+
const usersInRoom = this.roomRepository.getUsersInRoom(user.roomUUID);
17+
console.log(usersInRoom);
1618

17-
server.to(user.roomUUID).emit(EVENT.RECEIVE_SIGNALING, { userUUID: user.uuid });
19+
client.to(user.roomUUID).emit(EVENT.RECEIVE_SIGNALING, { userUUID: user.uuid });
1820
return {};
1921
}
2022

@@ -27,10 +29,12 @@ export class WebrtcService {
2729
connectSignal: WebrtcBaseDto;
2830
eventType: EVENT;
2931
}) {
30-
const { opponentId } = connectSignal;
32+
const { myId, opponentId } = connectSignal;
3133
const opponentClientId = this.roomRepository.getClientIdByUser(opponentId);
3234

33-
server.to(opponentClientId).emit(eventType, { connectSignal });
35+
server
36+
.to(opponentClientId)
37+
.emit(eventType, { ...connectSignal, myId: opponentId, opponentId: myId });
3438
return {};
3539
}
3640

frontend/src/hooks/useWebRTCSignaling.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,38 @@ const useWebRTCSignaling = (
109109
const startConnection = async (myId) => {
110110
await getMyStream(myId);
111111

112-
socket.on('receive_signaling', async (opponentId) => {
112+
socket.on('receive_signaling', async ({ userUUID: opponentId }) => {
113+
console.log('시그널 받음', '상대방', opponentId, '나', myId);
113114
const newConnection = makeConnection(myId, opponentId);
114115
const offer = await newConnection.createOffer();
115116
newConnection.setLocalDescription(offer);
116117

118+
console.log('offer 보냄', offer, opponentId);
119+
117120
socket.emit('offer', { offer, myId, opponentId });
118121
});
119122

120-
socket.on('offer', async ({ offer, opponentId }) => {
123+
socket.on('offer', async ({ offer, myId, opponentId }) => {
124+
console.log('offer 받음', offer);
121125
const newConnection = makeConnection(myId, opponentId);
122126
newConnection.setRemoteDescription(offer);
123127
const answer = await newConnection.createAnswer();
124128
newConnection.setLocalDescription(answer);
125129

130+
console.log('answer 보냄');
131+
126132
socket.emit('answer', { answer, myId, opponentId });
127133
});
128134

129135
socket.on('answer', ({ answer, opponentId }) => {
136+
// console.log(answer, opponentId);
137+
console.log('answer 받음');
130138
connectionListRef.current.get(opponentId).connection.setRemoteDescription(answer);
131139
});
132140

133141
socket.on('icecandidate', ({ icecandidate, opponentId }) => {
142+
// console.log(icecandidate, opponentId);
143+
console.log('ice 받음');
134144
connectionListRef.current.get(opponentId).connection.addIceCandidate(icecandidate);
135145
});
136146

@@ -145,7 +155,8 @@ const useWebRTCSignaling = (
145155
* 그 후, connectionListRef과 외부 webRTCUserList를 업데이트합니다.
146156
* @param closeId 서버가 보낸 나간 UserId
147157
*/
148-
const closeConnection = (closeId) => {
158+
const closeConnection = ({ userUUID: closeId }) => {
159+
console.log(closeId);
149160
const oldStream = webRTCUserList.get(closeId).stream;
150161
const oldConnection = webRTCUserList.get(closeId).connection;
151162

0 commit comments

Comments
 (0)