From 818ac97e5a65fb98a2ddbc7bc564c9bd784e6557 Mon Sep 17 00:00:00 2001 From: clemcle81500 Date: Mon, 2 Jun 2025 16:49:32 +0200 Subject: [PATCH] ajout des ws --- ...ocketConfig.kt => MatchWebSocketConfig.kt} | 4 +- .../config/PlayerWebSocketConfig.kt | 30 +++++++ .../config/RefereeWebSocketConfig.kt | 30 +++++++ .../fencerjudgeback/entities/MatchBean.kt | 2 +- .../repositories/MatchRepository.kt | 6 ++ .../fencerjudgeback/services/MatchService.kt | 8 +- .../controllers/MatchWebSocketController.kt | 18 ++-- ...tHTML.html => testMatchWebSocketHTML.html} | 2 +- .../static/testPlayerMatchWebSocketHTML.html | 87 +++++++++++++++++++ .../static/testRefereeWebSocketHTML.html | 87 +++++++++++++++++++ 10 files changed, 260 insertions(+), 14 deletions(-) rename FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/{WebSocketConfig.kt => MatchWebSocketConfig.kt} (90%) create mode 100644 FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/PlayerWebSocketConfig.kt create mode 100644 FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/RefereeWebSocketConfig.kt rename FencerJudgeBack/src/main/resources/static/{testWebSocketHTML.html => testMatchWebSocketHTML.html} (98%) create mode 100644 FencerJudgeBack/src/main/resources/static/testPlayerMatchWebSocketHTML.html create mode 100644 FencerJudgeBack/src/main/resources/static/testRefereeWebSocketHTML.html diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/WebSocketConfig.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/MatchWebSocketConfig.kt similarity index 90% rename from FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/WebSocketConfig.kt rename to FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/MatchWebSocketConfig.kt index 341b6f1..80d1341 100644 --- a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/WebSocketConfig.kt +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/MatchWebSocketConfig.kt @@ -6,11 +6,11 @@ import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBr import org.springframework.web.socket.config.annotation.StompEndpointRegistry import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer -const val CHANNEL_NAME: String = "/ws/topic" +const val CHANNEL_MATCH_NAME: String = "/ws/topic/match" @Configuration @EnableWebSocketMessageBroker -open class WebSocketConfig : WebSocketMessageBrokerConfigurer { +open class MatchWebSocketConfig : WebSocketMessageBrokerConfigurer { override fun configureMessageBroker(registry: MessageBrokerRegistry) { // Enable a simple memory-based message broker to send messages to clients diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/PlayerWebSocketConfig.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/PlayerWebSocketConfig.kt new file mode 100644 index 0000000..012e601 --- /dev/null +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/PlayerWebSocketConfig.kt @@ -0,0 +1,30 @@ +package fr.teamflash.fencerjudgeback.config + +import org.springframework.context.annotation.Configuration +import org.springframework.messaging.simp.config.MessageBrokerRegistry +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker +import org.springframework.web.socket.config.annotation.StompEndpointRegistry +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer + +const val CHANNEL_PLAYER_NAME: String = "/ws/topic/match" + +@Configuration +@EnableWebSocketMessageBroker +open class PlayerWebSocketConfig : WebSocketMessageBrokerConfigurer { + + override fun configureMessageBroker(registry: MessageBrokerRegistry) { + // Enable a simple memory-based message broker to send messages to clients + // Prefix for messages FROM server TO client + registry.enableSimpleBroker(CHANNEL_PLAYER_NAME) + + // Prefix for messages FROM client TO server + registry.setApplicationDestinationPrefixes("/ws") + } + + override fun registerStompEndpoints(registry: StompEndpointRegistry) { + // Register the "/ws" endpoint, enabling SockJS fallback options + registry.addEndpoint("/ws/players-app") + .setAllowedOriginPatterns("*") // Allow connections from any origin (adjust for production) + .withSockJS() + } +} \ No newline at end of file diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/RefereeWebSocketConfig.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/RefereeWebSocketConfig.kt new file mode 100644 index 0000000..9d4cf59 --- /dev/null +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/RefereeWebSocketConfig.kt @@ -0,0 +1,30 @@ +package fr.teamflash.fencerjudgeback.config + +import org.springframework.context.annotation.Configuration +import org.springframework.messaging.simp.config.MessageBrokerRegistry +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker +import org.springframework.web.socket.config.annotation.StompEndpointRegistry +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer + +const val CHANNEL_REFEREE_NAME: String = "/ws/topic/referee" + +@Configuration +@EnableWebSocketMessageBroker +open class RefereeWebSocketConfig : WebSocketMessageBrokerConfigurer { + + override fun configureMessageBroker(registry: MessageBrokerRegistry) { + // Enable a simple memory-based message broker to send messages to clients + // Prefix for messages FROM server TO client + registry.enableSimpleBroker(CHANNEL_REFEREE_NAME) + + // Prefix for messages FROM client TO server + registry.setApplicationDestinationPrefixes("/ws") + } + + override fun registerStompEndpoints(registry: StompEndpointRegistry) { + // Register the "/ws" endpoint, enabling SockJS fallback options + registry.addEndpoint("/ws/referees-app") + .setAllowedOriginPatterns("*") // Allow connections from any origin (adjust for production) + .withSockJS() + } +} \ No newline at end of file diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/entities/MatchBean.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/entities/MatchBean.kt index 0f95236..87ccf3e 100644 --- a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/entities/MatchBean.kt +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/entities/MatchBean.kt @@ -15,7 +15,7 @@ data class MatchBean( @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "match_sequence") @SequenceGenerator(name = "match_sequence", sequenceName = "match_seq", allocationSize = 1) - val id:Long?=null, + var id:Long?=null, val weapon:String?=null, val country:String?=null, val city:String?=null, diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/repositories/MatchRepository.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/repositories/MatchRepository.kt index 728d059..9ce2449 100644 --- a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/repositories/MatchRepository.kt +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/repositories/MatchRepository.kt @@ -6,4 +6,10 @@ import org.springframework.stereotype.Repository @Repository interface MatchRepository: JpaRepository { + fun getAll(): List { + return listOf( + MatchBean(1, "Fleuret", "France", "Paris", 1, 2, 3, 10, 12, "16-08-2004", "terminé") + ) + // return this.findAll() + } } \ No newline at end of file diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/services/MatchService.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/services/MatchService.kt index 1e5f24b..83ec52a 100644 --- a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/services/MatchService.kt +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/services/MatchService.kt @@ -15,7 +15,7 @@ class MatchService( // Obtenir tous les matchs (public) fun getAll() : List { println("MatchService.getMatchs") - return matchRepository.findAll() + return matchRepository.getAll() } // Obtenir un match par id (public) @@ -33,7 +33,7 @@ class MatchService( // Obtenir un ou plusieurs match(s) par joueurs (id) (public) fun getByPlayers(player1ID: Long?, player2ID: Long?): List { println("MatchService.getMatchByPlayers : $player1ID - $player2ID") - return matchRepository.findAll().filter { it.player1ID == player1ID && it.player2ID == player2ID } + return matchRepository.getAll().filter { it.player1ID == player1ID && it.player2ID == player2ID } } // Ajouter un match (admin) @@ -81,14 +81,14 @@ class MatchService( fun getMatchesByCity(city: String): List? { println("MatchService.getMatchesByCity : $city") - return matchRepository.findAll() + return matchRepository.getAll() .filter { it.city == city } } fun getMatchesByCountry(country: String): List? { println("MatchService.getMatchesByCountry : $country") - return matchRepository.findAll() + return matchRepository.getAll() .filter { it.country == country } } diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/websocket/controllers/MatchWebSocketController.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/websocket/controllers/MatchWebSocketController.kt index e75518d..1b86f73 100644 --- a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/websocket/controllers/MatchWebSocketController.kt +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/websocket/controllers/MatchWebSocketController.kt @@ -1,6 +1,6 @@ package fr.teamflash.fencerjudgeback.websocket.controllers -import fr.teamflash.fencerjudgeback.config.CHANNEL_NAME +import fr.teamflash.fencerjudgeback.config.CHANNEL_MATCH_NAME import fr.teamflash.fencerjudgeback.entities.MatchBean import fr.teamflash.fencerjudgeback.services.MatchService import fr.teamflash.fencerjudgeback.websocket.models.MatchUpdateMessage @@ -9,6 +9,7 @@ import org.springframework.messaging.handler.annotation.MessageMapping import org.springframework.messaging.simp.SimpMessagingTemplate import org.springframework.messaging.simp.stomp.StompHeaderAccessor import org.springframework.stereotype.Controller +import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.socket.messaging.SessionSubscribeEvent @@ -19,6 +20,7 @@ class MatchWebSocketController( private val messagingTemplate: SimpMessagingTemplate ) { + private var mainId:Long? = 1 private val messageHistory = ArrayList() @MessageMapping("/all") @@ -28,7 +30,7 @@ class MatchWebSocketController( // Envoyer la liste des messages sur le channel //Si la variable est dans le même package il faut enlever WebSocketConfig. - messagingTemplate.convertAndSend(CHANNEL_NAME, messageHistory) + messagingTemplate.convertAndSend(CHANNEL_MATCH_NAME, messageHistory) } /** @@ -69,13 +71,14 @@ class MatchWebSocketController( @EventListener fun handleWebSocketSubscribeListener(event: SessionSubscribeEvent) { val headerAccessor = StompHeaderAccessor.wrap(event.message) - if (CHANNEL_NAME == headerAccessor.destination) { - messagingTemplate.convertAndSend(CHANNEL_NAME, messageHistory) + if (CHANNEL_MATCH_NAME == headerAccessor.destination) { + messagingTemplate.convertAndSend(CHANNEL_MATCH_NAME, messageHistory) println("Lancement...") } } - fun addPoint(match:MatchBean, playerId:Long) { + @MessageMapping("/plusPoint/{playerId}") + fun addPoint(match:MatchBean, @PathVariable playerId:Long) { when (playerId) { match.player1ID -> match.score1 += 1 match.player2ID -> match.score2 += 1 @@ -84,7 +87,8 @@ class MatchWebSocketController( broadcastMatchUpdate(match) } - fun minusPoint(match:MatchBean, playerId:Long) { + @MessageMapping("/minusPoint/{playerId}") + fun minusPoint(match:MatchBean, @PathVariable playerId:Long) { when (playerId) { match.player1ID -> if (match.score1 > 0) match.score1 -= 1 match.player2ID -> if (match.score2 > 0) match.score2 -= 1 @@ -95,8 +99,10 @@ class MatchWebSocketController( @MessageMapping("/add") fun addMatchtoMainList(match:MatchBean) { + match.id = mainId; matchService.addMatch(match) messageHistory.add(match) broadcastMatchUpdate(match, MatchUpdateMessage.UpdateType.NEW_MATCH) + mainId = mainId?.plus(1) } } \ No newline at end of file diff --git a/FencerJudgeBack/src/main/resources/static/testWebSocketHTML.html b/FencerJudgeBack/src/main/resources/static/testMatchWebSocketHTML.html similarity index 98% rename from FencerJudgeBack/src/main/resources/static/testWebSocketHTML.html rename to FencerJudgeBack/src/main/resources/static/testMatchWebSocketHTML.html index 1009354..2819a41 100644 --- a/FencerJudgeBack/src/main/resources/static/testWebSocketHTML.html +++ b/FencerJudgeBack/src/main/resources/static/testMatchWebSocketHTML.html @@ -48,7 +48,7 @@ + + + + + + diff --git a/FencerJudgeBack/src/main/resources/static/testRefereeWebSocketHTML.html b/FencerJudgeBack/src/main/resources/static/testRefereeWebSocketHTML.html new file mode 100644 index 0000000..87d370b --- /dev/null +++ b/FencerJudgeBack/src/main/resources/static/testRefereeWebSocketHTML.html @@ -0,0 +1,87 @@ + + + + + Test WebSocket Match + + + + +

Test WebSocket Match

+ +
Connexion...
+ +
+ + + + +
+ +
+ + + +
+ + + + + + + + + +