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

[GWL-280] 매칭 API의 자신을 표시하는 myPublicID 생성, events gateway 로깅 #281

Merged
merged 3 commits into from
Dec 7, 2023
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
10 changes: 9 additions & 1 deletion BackEnd/src/live-workouts/events/events.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ExtensionWebSocketService } from './extensionWebSocket.service';
import { WetriWebSocket, WetriServer } from './types/custom-websocket.type';
import { AuthService } from '../../auth/auth.service';
import { CheckMatchingDto } from './dto/checkMatching.dto';
import {Logger} from "@nestjs/common";

@WebSocketGateway(3003)
export class EventsGateway
Expand All @@ -28,6 +29,7 @@ export class EventsGateway
}

async handleConnection(client: WetriWebSocket, ...args: any[]) {
Logger.log("연결 성공");
const { authorization, roomid } = args[0].headers;
if (!(await this.authService.verifyWs(authorization, client))) {
client.close();
Expand All @@ -43,12 +45,16 @@ export class EventsGateway
client.close();
return;
}

Logger.log("연결 검증 성공");

this.extensionWebSocketService.webSocket(client, this.server);
client.join(roomid);
}

@SubscribeMessage('workout_session')
onWorkoutSession(client: WetriWebSocket, data: any): void {
Logger.log(`응답 받은 답 : ${data}`);
if (!this.eventsService.checkMsgRoomId(data)) {
client.wemit('workout_session', 'data에 roomId를 포함시켜주세요.');
} else {
Expand All @@ -60,5 +66,7 @@ export class EventsGateway
}
}

handleDisconnect(client: any) {}
handleDisconnect(client: any) {
Logger.log("연결 종료");
}
}
4 changes: 4 additions & 0 deletions BackEnd/src/live-workouts/matches/dto/random-match.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class RandomMatch {
@ApiProperty({ example: true })
matched: boolean;

@IsString()
@ApiProperty({ example: 'uuid' })
myPublicId?: string;

@IsString()
@ApiProperty({ example: 'yyyy-mm-dd hh:mm:ss' })
liveWorkoutStartTime?: string;
Expand Down
28 changes: 0 additions & 28 deletions BackEnd/src/live-workouts/matches/matches.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,6 @@ describe('MatchesService', () => {
});
});

describe('makeWebSocketRoom 메서드 검증', () => {
it('달리기 종목에 대기인원이 2명이고일 때 트렌잭션이 잘 작동하는지 테스트', async () => {
const workoutId = 1;
const waitingUsers = 2;
const serializedUsers = [
JSON.stringify({ publicId: 'User1' }),
JSON.stringify({ publicId: 'User2' }),
];
lrange.mockResolvedValue(serializedUsers);

const result = await service['makeWebSocketRoom'](
workoutId,
waitingUsers,
);

expect(lrange).toHaveBeenCalledWith(
`matching:${workoutId}`,
0,
waitingUsers - 1,
);
expect(multi().set).toHaveBeenCalled();
expect(multi().expire).toHaveBeenCalled();
expect(multi().ltrim).toHaveBeenCalled();
expect(multi().exec).toHaveBeenCalled();
expect(result).toHaveProperty('matched', true);
});
});

describe('matchingAlgorithm 메서드 검증', () => {
it('대기 큐에 2명이 존재하고, 기다린 시간이 20초라면?', () => {
const queueLength = 2;
Expand Down
10 changes: 6 additions & 4 deletions BackEnd/src/live-workouts/matches/matches.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class MatchesService {
);
return {
matched: true,
myPublicId: publicId,
roomId: roomId,
liveWorkoutStartTime: liveWorkoutStartTimeUTC,
peers: profiles,
Expand All @@ -79,17 +80,17 @@ export class MatchesService {
const waitingUsers = this.matchingAlgorithm(waitingLength, waitingTime);

if (waitingUsers >= MIN_USERS) {
return await this.makeWebSocketRoom(workoutId, waitingUsers);
return await this.makeWebSocketRoom(workoutId, waitingUsers, publicId);
}
return {
matched: false
};
}

private async makeWebSocketRoom(
workoutId: number,
waitingUsers: number,
): Promise<RandomMatch> {
workoutId: number,
waitingUsers: number,
myPublicId: any): Promise<RandomMatch> {
const roomId: string = `match:${workoutId}:${uuidv4()}`;

const serializedUsers: string[] = await this.redis.lrange(
Expand Down Expand Up @@ -133,6 +134,7 @@ export class MatchesService {
return {
matched: true,
roomId: roomId,
myPublicId: myPublicId,
liveWorkoutStartTime: liveWorkoutStartTimeUTC,
peers: profiles,
};
Expand Down
Loading