Merge pull request 'back-clement-ws' (#7) from back-clement-ws into back-lucien

Reviewed-on: #7
This commit is contained in:
ExostFlash 2025-06-02 13:56:32 +00:00
commit de63550429
4 changed files with 11 additions and 10 deletions

View file

@ -23,7 +23,7 @@ open class WebSocketConfig : WebSocketMessageBrokerConfigurer {
override fun registerStompEndpoints(registry: StompEndpointRegistry) { override fun registerStompEndpoints(registry: StompEndpointRegistry) {
// Register the "/ws" endpoint, enabling SockJS fallback options // Register the "/ws" endpoint, enabling SockJS fallback options
registry.addEndpoint("/ws") registry.addEndpoint("/ws/matches-app")
.setAllowedOriginPatterns("*") // Allow connections from any origin (adjust for production) .setAllowedOriginPatterns("*") // Allow connections from any origin (adjust for production)
.withSockJS() .withSockJS()
} }

View file

@ -6,7 +6,6 @@ import fr.teamflash.fencerjudgeback.services.MatchService
import fr.teamflash.fencerjudgeback.websocket.models.MatchUpdateMessage import fr.teamflash.fencerjudgeback.websocket.models.MatchUpdateMessage
import org.springframework.context.event.EventListener import org.springframework.context.event.EventListener
import org.springframework.messaging.handler.annotation.MessageMapping import org.springframework.messaging.handler.annotation.MessageMapping
import org.springframework.messaging.handler.annotation.SendTo
import org.springframework.messaging.simp.SimpMessagingTemplate import org.springframework.messaging.simp.SimpMessagingTemplate
import org.springframework.messaging.simp.stomp.StompHeaderAccessor import org.springframework.messaging.simp.stomp.StompHeaderAccessor
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
@ -14,7 +13,7 @@ import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.socket.messaging.SessionSubscribeEvent import org.springframework.web.socket.messaging.SessionSubscribeEvent
@Controller @Controller
@RequestMapping("/ws") @RequestMapping("/ws/matches")
class MatchWebSocketController( class MatchWebSocketController(
private val matchService: MatchService, private val matchService: MatchService,
private val messagingTemplate: SimpMessagingTemplate private val messagingTemplate: SimpMessagingTemplate
@ -22,7 +21,7 @@ class MatchWebSocketController(
private val messageHistory = ArrayList<MatchBean>() private val messageHistory = ArrayList<MatchBean>()
@MessageMapping("/matches") @MessageMapping("/all")
fun receiveMessage(message: MatchBean) { fun receiveMessage(message: MatchBean) {
println("/ws/matches $message") println("/ws/matches $message")
messageHistory.add(message) messageHistory.add(message)
@ -37,8 +36,7 @@ class MatchWebSocketController(
* Client sends to: /app/match.update * Client sends to: /app/match.update
* Server broadcasts to: /topic/match.updates * Server broadcasts to: /topic/match.updates
*/ */
@MessageMapping("/match.update") @MessageMapping("/update")
@SendTo("/topic/match.updates")
fun handleMatchUpdate(matchUpdateMessage: MatchUpdateMessage): MatchUpdateMessage { fun handleMatchUpdate(matchUpdateMessage: MatchUpdateMessage): MatchUpdateMessage {
// Create a MatchBean from the update message // Create a MatchBean from the update message
val matchBean = MatchBean( val matchBean = MatchBean(
@ -73,6 +71,7 @@ class MatchWebSocketController(
val headerAccessor = StompHeaderAccessor.wrap(event.message) val headerAccessor = StompHeaderAccessor.wrap(event.message)
if (CHANNEL_NAME == headerAccessor.destination) { if (CHANNEL_NAME == headerAccessor.destination) {
messagingTemplate.convertAndSend(CHANNEL_NAME, messageHistory) messagingTemplate.convertAndSend(CHANNEL_NAME, messageHistory)
println("Lancement...")
} }
} }
@ -94,8 +93,10 @@ class MatchWebSocketController(
broadcastMatchUpdate(match) broadcastMatchUpdate(match)
} }
@MessageMapping("/add")
fun addMatchtoMainList(match:MatchBean) { fun addMatchtoMainList(match:MatchBean) {
matchService.addMatch(match) matchService.addMatch(match)
messageHistory.add(match)
broadcastMatchUpdate(match, MatchUpdateMessage.UpdateType.NEW_MATCH) broadcastMatchUpdate(match, MatchUpdateMessage.UpdateType.NEW_MATCH)
} }
} }

View file

@ -1,6 +1,5 @@
package fr.teamflash.fencerjudgeback.websocket.controllers package fr.teamflash.fencerjudgeback.websocket.controllers
import fr.teamflash.fencerjudgeback.config.CHANNEL_NAME
import fr.teamflash.fencerjudgeback.entities.PlayerBean import fr.teamflash.fencerjudgeback.entities.PlayerBean
import fr.teamflash.fencerjudgeback.services.PlayerService import fr.teamflash.fencerjudgeback.services.PlayerService
import fr.teamflash.fencerjudgeback.websocket.models.PlayerUpdateMessage import fr.teamflash.fencerjudgeback.websocket.models.PlayerUpdateMessage
@ -64,6 +63,7 @@ class PlayerWebSocketController(
} }
} }
@MessageMapping
fun addPlayerToMainList(playerBean: PlayerBean) { fun addPlayerToMainList(playerBean: PlayerBean) {
playerService.addPlayer(playerBean) playerService.addPlayer(playerBean)
broadcastPlayerUpdate(playerBean, PlayerUpdateMessage.UpdateType.PLAYER_CREATE) broadcastPlayerUpdate(playerBean, PlayerUpdateMessage.UpdateType.PLAYER_CREATE)

View file

@ -47,8 +47,8 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
<script> <script>
const stompClient = Stomp.over(new SockJS('/ws')); const stompClient = Stomp.over(new SockJS('/ws/matches-app'));
const channel = "/topic/match.updates"; const channel = "/ws/topic";
stompClient.connect({}, function () { stompClient.connect({}, function () {
stompClient.subscribe(channel, function (message) { stompClient.subscribe(channel, function (message) {
@ -72,7 +72,7 @@
date: document.getElementById("matchDate").value date: document.getElementById("matchDate").value
}; };
stompClient.send("/app/match.update", {}, JSON.stringify(match)); stompClient.send("/ws/matches/add", {}, JSON.stringify(match));
} }
function displayMessage(match) { function displayMessage(match) {