-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement player disconnection handling; update WebSocket service and…
… GameService to manage room state
- Loading branch information
Showing
5 changed files
with
213 additions
and
23 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
backend/src/main/java/com/game/config/WebSocketEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.game.config; | ||
|
||
import com.game.service.GameService; | ||
|
||
import java.security.Principal; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.messaging.SessionDisconnectEvent; | ||
|
||
@Component | ||
public class WebSocketEventListener { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(WebSocketEventListener.class); | ||
|
||
@Autowired | ||
private GameService gameService; | ||
|
||
@Autowired | ||
private SimpMessagingTemplate messagingTemplate; | ||
|
||
@EventListener | ||
public void handleWebSocketDisconnect(SessionDisconnectEvent event) { | ||
StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage()); | ||
String username = Optional.ofNullable(headerAccessor.getUser()) | ||
.map(Principal::getName) | ||
.orElse(null); | ||
String roomId = gameService.getRoomOfPlayer(username); | ||
|
||
if (roomId != null && username != null) { | ||
if (gameService.removePlayerFromRoom(roomId, username)) { | ||
logger.info("Player {} disconnected from room {}", username, roomId); | ||
messagingTemplate.convertAndSend("/topic/room/" + roomId, | ||
Map.of("type", "player_disconnected", "username", username, "roomId", roomId)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters