ajout des ws
This commit is contained in:
parent
de63550429
commit
818ac97e5a
10 changed files with 260 additions and 14 deletions
|
|
@ -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
|
||||
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -6,4 +6,10 @@ import org.springframework.stereotype.Repository
|
|||
|
||||
@Repository
|
||||
interface MatchRepository: JpaRepository<MatchBean, Long> {
|
||||
fun getAll(): List<MatchBean> {
|
||||
return listOf(
|
||||
MatchBean(1, "Fleuret", "France", "Paris", 1, 2, 3, 10, 12, "16-08-2004", "terminé")
|
||||
)
|
||||
// return this.findAll()
|
||||
}
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ class MatchService(
|
|||
// Obtenir tous les matchs (public)
|
||||
fun getAll() : List<MatchBean> {
|
||||
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<MatchBean> {
|
||||
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<MatchBean>? {
|
||||
println("MatchService.getMatchesByCity : $city")
|
||||
|
||||
return matchRepository.findAll()
|
||||
return matchRepository.getAll()
|
||||
.filter { it.city == city }
|
||||
}
|
||||
|
||||
fun getMatchesByCountry(country: String): List<MatchBean>? {
|
||||
println("MatchService.getMatchesByCountry : $country")
|
||||
|
||||
return matchRepository.findAll()
|
||||
return matchRepository.getAll()
|
||||
.filter { it.country == country }
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<MatchBean>()
|
||||
|
||||
@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)
|
||||
}
|
||||
}
|
||||
|
|
@ -48,7 +48,7 @@
|
|||
|
||||
<script>
|
||||
const stompClient = Stomp.over(new SockJS('/ws/matches-app'));
|
||||
const channel = "/ws/topic";
|
||||
const channel = "/ws/topic/match";
|
||||
|
||||
stompClient.connect({}, function () {
|
||||
stompClient.subscribe(channel, function (message) {
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Test WebSocket Match</title>
|
||||
<style>
|
||||
#messageArea {
|
||||
width: 100%;
|
||||
height: 300px;
|
||||
border: 1px solid #ddd;
|
||||
overflow-y: auto;
|
||||
padding: 10px;
|
||||
margin-bottom: 10px;
|
||||
font-family: monospace;
|
||||
}
|
||||
input, button {
|
||||
padding: 8px;
|
||||
margin: 4px;
|
||||
}
|
||||
.input-group {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Test WebSocket Match</h2>
|
||||
|
||||
<div id="messageArea">Connexion...</div>
|
||||
|
||||
<div class="input-group">
|
||||
<input type="number" id="matchId" placeholder="Match ID">
|
||||
<input type="number" id="player1Id" placeholder="Joueur 1 ID">
|
||||
<input type="number" id="player2Id" placeholder="Joueur 2 ID">
|
||||
<input type="number" id="refereeId" placeholder="Arbitre ID">
|
||||
</div>
|
||||
|
||||
<div class="input-group">
|
||||
<input type="number" id="score1" placeholder="Score Joueur 1">
|
||||
<input type="number" id="score2" placeholder="Score Joueur 2">
|
||||
<input type="datetime-local" id="matchDate">
|
||||
</div>
|
||||
|
||||
<button onclick="sendMatch()">Envoyer Match</button>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/sockjs-client/dist/sockjs.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
|
||||
|
||||
<script>
|
||||
const stompClient = Stomp.over(new SockJS('/ws/players-app'));
|
||||
const channel = "/ws/topic/player";
|
||||
|
||||
stompClient.connect({}, function () {
|
||||
stompClient.subscribe(channel, function (message) {
|
||||
const msg = JSON.parse(message.body);
|
||||
console.log("Match reçu :", msg);
|
||||
displayMessage(msg);
|
||||
});
|
||||
document.getElementById('messageArea').textContent = 'Connecté au WebSocket';
|
||||
}, function (error) {
|
||||
document.getElementById('messageArea').textContent = 'Erreur WebSocket : ' + error;
|
||||
});
|
||||
|
||||
function sendMatch() {
|
||||
const match = {
|
||||
matchId: parseInt(document.getElementById("matchId").value),
|
||||
player1Id: parseInt(document.getElementById("player1Id").value),
|
||||
player2Id: parseInt(document.getElementById("player2Id").value),
|
||||
refereeId: parseInt(document.getElementById("refereeId").value),
|
||||
score1: parseInt(document.getElementById("score1").value),
|
||||
score2: parseInt(document.getElementById("score2").value),
|
||||
date: document.getElementById("matchDate").value
|
||||
};
|
||||
|
||||
stompClient.send("/ws/matches/add", {}, JSON.stringify(match));
|
||||
}
|
||||
|
||||
function displayMessage(match) {
|
||||
const area = document.getElementById("messageArea");
|
||||
const div = document.createElement("div");
|
||||
div.textContent = `Match ${match.matchId}: ${match.player1Id} (${match.score1}) vs ${match.player2Id} (${match.score2})`;
|
||||
area.appendChild(div);
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Test WebSocket Match</title>
|
||||
<style>
|
||||
#messageArea {
|
||||
width: 100%;
|
||||
height: 300px;
|
||||
border: 1px solid #ddd;
|
||||
overflow-y: auto;
|
||||
padding: 10px;
|
||||
margin-bottom: 10px;
|
||||
font-family: monospace;
|
||||
}
|
||||
input, button {
|
||||
padding: 8px;
|
||||
margin: 4px;
|
||||
}
|
||||
.input-group {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Test WebSocket Match</h2>
|
||||
|
||||
<div id="messageArea">Connexion...</div>
|
||||
|
||||
<div class="input-group">
|
||||
<input type="number" id="matchId" placeholder="Match ID">
|
||||
<input type="number" id="player1Id" placeholder="Joueur 1 ID">
|
||||
<input type="number" id="player2Id" placeholder="Joueur 2 ID">
|
||||
<input type="number" id="refereeId" placeholder="Arbitre ID">
|
||||
</div>
|
||||
|
||||
<div class="input-group">
|
||||
<input type="number" id="score1" placeholder="Score Joueur 1">
|
||||
<input type="number" id="score2" placeholder="Score Joueur 2">
|
||||
<input type="datetime-local" id="matchDate">
|
||||
</div>
|
||||
|
||||
<button onclick="sendMatch()">Envoyer Match</button>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/sockjs-client/dist/sockjs.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
|
||||
|
||||
<script>
|
||||
const stompClient = Stomp.over(new SockJS('/ws/referees-app'));
|
||||
const channel = "/ws/topic/referee";
|
||||
|
||||
stompClient.connect({}, function () {
|
||||
stompClient.subscribe(channel, function (message) {
|
||||
const msg = JSON.parse(message.body);
|
||||
console.log("Match reçu :", msg);
|
||||
displayMessage(msg);
|
||||
});
|
||||
document.getElementById('messageArea').textContent = 'Connecté au WebSocket';
|
||||
}, function (error) {
|
||||
document.getElementById('messageArea').textContent = 'Erreur WebSocket : ' + error;
|
||||
});
|
||||
|
||||
function sendMatch() {
|
||||
const match = {
|
||||
matchId: parseInt(document.getElementById("matchId").value),
|
||||
player1Id: parseInt(document.getElementById("player1Id").value),
|
||||
player2Id: parseInt(document.getElementById("player2Id").value),
|
||||
refereeId: parseInt(document.getElementById("refereeId").value),
|
||||
score1: parseInt(document.getElementById("score1").value),
|
||||
score2: parseInt(document.getElementById("score2").value),
|
||||
date: document.getElementById("matchDate").value
|
||||
};
|
||||
|
||||
stompClient.send("/ws/matches/add", {}, JSON.stringify(match));
|
||||
}
|
||||
|
||||
function displayMessage(match) {
|
||||
const area = document.getElementById("messageArea");
|
||||
const div = document.createElement("div");
|
||||
div.textContent = `Match ${match.matchId}: ${match.player1Id} (${match.score1}) vs ${match.player2Id} (${match.score2})`;
|
||||
area.appendChild(div);
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Reference in a new issue