diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/MatchWebSocketConfig.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/MatchWebSocketConfig.kt new file mode 100644 index 0000000..6fc3650 --- /dev/null +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/MatchWebSocketConfig.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_MATCH_NAME: String = "/ws/topic/match" + +@Configuration +@EnableWebSocketMessageBroker +open class MatchWebSocketConfig : 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_MATCH_NAME) + + // Prefix for messages FROM client TO server + registry.setApplicationDestinationPrefixes("/ws/matches") + } + + override fun registerStompEndpoints(registry: StompEndpointRegistry) { + // Register the "/ws" endpoint, enabling SockJS fallback options + registry.addEndpoint("/ws/matches-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/WebSocketConfig.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/PlayerWebSocketConfig.kt similarity index 82% rename from FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/WebSocketConfig.kt rename to FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/PlayerWebSocketConfig.kt index 341b6f1..012e601 100644 --- a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/WebSocketConfig.kt +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/PlayerWebSocketConfig.kt @@ -6,16 +6,16 @@ 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_PLAYER_NAME: String = "/ws/topic/match" @Configuration @EnableWebSocketMessageBroker -open class WebSocketConfig : WebSocketMessageBrokerConfigurer { +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_NAME) + registry.enableSimpleBroker(CHANNEL_PLAYER_NAME) // Prefix for messages FROM client TO server registry.setApplicationDestinationPrefixes("/ws") @@ -23,7 +23,7 @@ open class WebSocketConfig : WebSocketMessageBrokerConfigurer { override fun registerStompEndpoints(registry: StompEndpointRegistry) { // Register the "/ws" endpoint, enabling SockJS fallback options - registry.addEndpoint("/ws/matches-app") + registry.addEndpoint("/ws/players-app") .setAllowedOriginPatterns("*") // Allow connections from any origin (adjust for production) .withSockJS() } 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/entities/MatchUpdateBean.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/entities/MatchUpdateBean.kt new file mode 100644 index 0000000..16059ac --- /dev/null +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/entities/MatchUpdateBean.kt @@ -0,0 +1,3 @@ +package fr.teamflash.fencerjudgeback.entities + +data class MatchUpdateBean(val match: MatchBean?=null, val playerId:Long?=null) \ No newline at end of file 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..bee6a2f 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,7 +1,8 @@ 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.entities.MatchUpdateBean import fr.teamflash.fencerjudgeback.services.MatchService import fr.teamflash.fencerjudgeback.websocket.models.MatchUpdateMessage import org.springframework.context.event.EventListener @@ -9,16 +10,15 @@ 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.RequestMapping import org.springframework.web.socket.messaging.SessionSubscribeEvent @Controller -@RequestMapping("/ws/matches") class MatchWebSocketController( private val matchService: MatchService, private val messagingTemplate: SimpMessagingTemplate ) { + private var mainId:Long? = 1 private val messageHistory = ArrayList() @MessageMapping("/all") @@ -28,7 +28,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,34 +69,50 @@ class MatchWebSocketController( @EventListener fun handleWebSocketSubscribeListener(event: SessionSubscribeEvent) { val headerAccessor = StompHeaderAccessor.wrap(event.message) - if (CHANNEL_NAME == headerAccessor.destination) { - messagingTemplate.convertAndSend(CHANNEL_NAME, messageHistory) - println("Lancement...") + if (CHANNEL_MATCH_NAME == headerAccessor.destination) { + messagingTemplate.convertAndSend(CHANNEL_MATCH_NAME, messageHistory) + println("Lancement du WebSocket") } } - fun addPoint(match:MatchBean, playerId:Long) { - when (playerId) { - match.player1ID -> match.score1 += 1 - match.player2ID -> match.score2 += 1 + @MessageMapping("/plusPoint") + fun addPoint(matchUpdateBean: MatchUpdateBean) { + val playerId = matchUpdateBean.playerId + val match = matchUpdateBean.match + print("plus") + if (match != null) { + print("minus") + when (playerId) { + match.player1ID -> if (match.score1 > 0) match.score1 -= 1 + match.player2ID -> if (match.score2 > 0) match.score2 -= 1 + } + matchService.updateMatch(match.id, match) + broadcastMatchUpdate(match) } - matchService.updateMatch(match.id, match) - broadcastMatchUpdate(match) } - fun minusPoint(match:MatchBean, playerId:Long) { - when (playerId) { - match.player1ID -> if (match.score1 > 0) match.score1 -= 1 - match.player2ID -> if (match.score2 > 0) match.score2 -= 1 + @MessageMapping("/minusPoint") + fun minusPoint(matchUpdateBean: MatchUpdateBean) { + val playerId = matchUpdateBean.playerId + val match = matchUpdateBean.match + print("minus") + if (match != null) { + when (playerId) { + match.player1ID -> if (match.score1 > 0) match.score1 -= 1 + match.player2ID -> if (match.score2 > 0) match.score2 -= 1 + } + matchService.updateMatch(match.id, match) + broadcastMatchUpdate(match) + } } - matchService.updateMatch(match.id, match) - broadcastMatchUpdate(match) - } @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) + println("Ajout du match $match.id en bdd") } } \ No newline at end of file diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/websocket/controllers/RefereeWebSocketController.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/websocket/controllers/RefereeWebSocketController.kt index 2b21601..6064fda 100644 --- a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/websocket/controllers/RefereeWebSocketController.kt +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/websocket/controllers/RefereeWebSocketController.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.RefereeBean import fr.teamflash.fencerjudgeback.services.RefereeService import fr.teamflash.fencerjudgeback.websocket.models.RefereeUpdateMessage diff --git a/FencerJudgeBack/src/main/resources/static/testMatchWebSocketHTML.html b/FencerJudgeBack/src/main/resources/static/testMatchWebSocketHTML.html new file mode 100644 index 0000000..05c5007 --- /dev/null +++ b/FencerJudgeBack/src/main/resources/static/testMatchWebSocketHTML.html @@ -0,0 +1,88 @@ + + + + + Test WebSocket Match + + + + +

Test WebSocket Match

+ +
Connexion...
+ +
+ + + +
+ +
+ + + +
+ + + + + + + + + + diff --git a/FencerJudgeBack/src/main/resources/static/testWebSocketHTML.html b/FencerJudgeBack/src/main/resources/static/testPlayerMatchWebSocketHTML.html similarity index 96% rename from FencerJudgeBack/src/main/resources/static/testWebSocketHTML.html rename to FencerJudgeBack/src/main/resources/static/testPlayerMatchWebSocketHTML.html index 1009354..15b617a 100644 --- a/FencerJudgeBack/src/main/resources/static/testWebSocketHTML.html +++ b/FencerJudgeBack/src/main/resources/static/testPlayerMatchWebSocketHTML.html @@ -47,8 +47,8 @@ + + + + + +