44import com .example .be .repository .RoomRepository ;
55import com .example .be .web .dto .OpenviduDTO ;
66import io .livekit .server .*;
7+ import livekit .LivekitModels ;
78import lombok .RequiredArgsConstructor ;
9+ import lombok .extern .slf4j .Slf4j ;
810import org .springframework .beans .factory .annotation .Value ;
911import org .springframework .http .ResponseEntity ;
1012import org .springframework .web .bind .annotation .*;
1315
1416import livekit .LivekitWebhook .WebhookEvent ;
1517
18+ @ Slf4j
1619@ CrossOrigin (origins = "*" )
1720@ RestController
1821@ RequestMapping ("/api/v1/video" )
@@ -51,10 +54,11 @@ public ResponseEntity<String> receiveWebhook(@RequestHeader("Authorization") Str
5154 try {
5255 WebhookEvent event = webhookReceiver .receive (body , authHeader );
5356 String roomName = event .getRoom ().getName ();
57+ int roomParticipantCount = event .getRoom ().getNumParticipants ();
5458 long createAt = event .getRoom ().getCreationTime ();
5559
56- System . out . println ("LiveKit Webhook Event: " + event .getEvent ());
57- System . out . println ("Room Name: " + roomName );
60+ log . info ("LiveKit Webhook Event: {}" , event .getEvent ());
61+ log . info ("Room Name:{}" , roomName );
5862
5963 // 이벤트 타입에 따른 처리 - event.getEvent() 사용
6064 String eventType = event .getEvent ();
@@ -67,39 +71,35 @@ public ResponseEntity<String> receiveWebhook(@RequestHeader("Authorization") Str
6771 Room newRoom = Room .builder ()
6872 .title (roomName )
6973 .createDate (createAt )
70- .participantCount (0 )
74+ .participantCount (roomParticipantCount )
7175 .maxParticipants (10 ) // 기본값 설정
7276 .build ();
7377 roomRepository .save (newRoom );
74- System .out .println ("Room created: " + roomName );
7578 }
7679 }
7780 case "participant_joined" -> {
7881 Room room = roomRepository .findByTitle (roomName );
7982 if (room != null ) {
80- room .setParticipantCount (room . getParticipantCount () + 1 );
83+ room .setParticipantCount (roomParticipantCount );
8184 roomRepository .save (room );
82- System . out . println ("Participant joined. Current count: " + room . getParticipantCount () );
85+ log . info ("Participant joined. Current count: {}" , roomParticipantCount );
8386 }
8487 }
8588 case "participant_left" -> {
8689 Room room = roomRepository .findByTitle (roomName );
87- if (room != null ) {
88- room .setParticipantCount (room .getParticipantCount () - 1 );
89- roomRepository .save (room );
90- }
91- if (room != null && room .getParticipantCount () > 0 ) {
90+ if (room != null && roomParticipantCount > 0 ) {
91+ room .setParticipantCount (roomParticipantCount );
9292 roomRepository .save (room );
93- System . out . println ("Participant left. Current count: " + room . getParticipantCount () );
93+ log . info ("Participant left now . Current count: {}" , roomParticipantCount );
9494 }
95- else if (room != null && room . getParticipantCount () == 0 ) {
95+ else if (room != null && roomParticipantCount == 0 ) {
9696 roomRepository .delete (room );
97- System . out . println ("Room deleted: " + roomName );
97+ log . info ("Room deleted: {}" , roomName );
9898 }
9999 }
100100 }
101101 } catch (Exception e ) {
102- System . err . println ("Error validating webhook event: " + e .getMessage ());
102+ log . info ("Error validating webhook event: {}" , e .getMessage ());
103103 e .printStackTrace ();
104104 return ResponseEntity .badRequest ().body ("Invalid webhook" );
105105 }
0 commit comments