Avancée sur ws
This commit is contained in:
parent
818ac97e5a
commit
608e0f7680
8 changed files with 63 additions and 51 deletions
|
|
@ -15,10 +15,10 @@ open class MatchWebSocketConfig : WebSocketMessageBrokerConfigurer {
|
||||||
override fun configureMessageBroker(registry: MessageBrokerRegistry) {
|
override fun configureMessageBroker(registry: MessageBrokerRegistry) {
|
||||||
// Enable a simple memory-based message broker to send messages to clients
|
// Enable a simple memory-based message broker to send messages to clients
|
||||||
// Prefix for messages FROM server TO client
|
// Prefix for messages FROM server TO client
|
||||||
registry.enableSimpleBroker(CHANNEL_NAME)
|
registry.enableSimpleBroker(CHANNEL_MATCH_NAME)
|
||||||
|
|
||||||
// Prefix for messages FROM client TO server
|
// Prefix for messages FROM client TO server
|
||||||
registry.setApplicationDestinationPrefixes("/ws")
|
registry.setApplicationDestinationPrefixes("/ws/matches")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun registerStompEndpoints(registry: StompEndpointRegistry) {
|
override fun registerStompEndpoints(registry: StompEndpointRegistry) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
package fr.teamflash.fencerjudgeback.entities
|
||||||
|
|
||||||
|
data class MatchUpdateBean(val match: MatchBean?=null, val playerId:Long?=null)
|
||||||
|
|
@ -6,10 +6,4 @@ import org.springframework.stereotype.Repository
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
interface MatchRepository: JpaRepository<MatchBean, Long> {
|
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)
|
// Obtenir tous les matchs (public)
|
||||||
fun getAll() : List<MatchBean> {
|
fun getAll() : List<MatchBean> {
|
||||||
println("MatchService.getMatchs")
|
println("MatchService.getMatchs")
|
||||||
return matchRepository.getAll()
|
return matchRepository.findAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Obtenir un match par id (public)
|
// Obtenir un match par id (public)
|
||||||
|
|
@ -33,7 +33,7 @@ class MatchService(
|
||||||
// Obtenir un ou plusieurs match(s) par joueurs (id) (public)
|
// Obtenir un ou plusieurs match(s) par joueurs (id) (public)
|
||||||
fun getByPlayers(player1ID: Long?, player2ID: Long?): List<MatchBean> {
|
fun getByPlayers(player1ID: Long?, player2ID: Long?): List<MatchBean> {
|
||||||
println("MatchService.getMatchByPlayers : $player1ID - $player2ID")
|
println("MatchService.getMatchByPlayers : $player1ID - $player2ID")
|
||||||
return matchRepository.getAll().filter { it.player1ID == player1ID && it.player2ID == player2ID }
|
return matchRepository.findAll().filter { it.player1ID == player1ID && it.player2ID == player2ID }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ajouter un match (admin)
|
// Ajouter un match (admin)
|
||||||
|
|
@ -81,14 +81,14 @@ class MatchService(
|
||||||
fun getMatchesByCity(city: String): List<MatchBean>? {
|
fun getMatchesByCity(city: String): List<MatchBean>? {
|
||||||
println("MatchService.getMatchesByCity : $city")
|
println("MatchService.getMatchesByCity : $city")
|
||||||
|
|
||||||
return matchRepository.getAll()
|
return matchRepository.findAll()
|
||||||
.filter { it.city == city }
|
.filter { it.city == city }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getMatchesByCountry(country: String): List<MatchBean>? {
|
fun getMatchesByCountry(country: String): List<MatchBean>? {
|
||||||
println("MatchService.getMatchesByCountry : $country")
|
println("MatchService.getMatchesByCountry : $country")
|
||||||
|
|
||||||
return matchRepository.getAll()
|
return matchRepository.findAll()
|
||||||
.filter { it.country == country }
|
.filter { it.country == country }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package fr.teamflash.fencerjudgeback.websocket.controllers
|
||||||
|
|
||||||
import fr.teamflash.fencerjudgeback.config.CHANNEL_MATCH_NAME
|
import fr.teamflash.fencerjudgeback.config.CHANNEL_MATCH_NAME
|
||||||
import fr.teamflash.fencerjudgeback.entities.MatchBean
|
import fr.teamflash.fencerjudgeback.entities.MatchBean
|
||||||
|
import fr.teamflash.fencerjudgeback.entities.MatchUpdateBean
|
||||||
import fr.teamflash.fencerjudgeback.services.MatchService
|
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
|
||||||
|
|
@ -9,12 +10,9 @@ import org.springframework.messaging.handler.annotation.MessageMapping
|
||||||
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
|
||||||
import org.springframework.web.bind.annotation.PathVariable
|
|
||||||
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/matches")
|
|
||||||
class MatchWebSocketController(
|
class MatchWebSocketController(
|
||||||
private val matchService: MatchService,
|
private val matchService: MatchService,
|
||||||
private val messagingTemplate: SimpMessagingTemplate
|
private val messagingTemplate: SimpMessagingTemplate
|
||||||
|
|
@ -73,29 +71,40 @@ class MatchWebSocketController(
|
||||||
val headerAccessor = StompHeaderAccessor.wrap(event.message)
|
val headerAccessor = StompHeaderAccessor.wrap(event.message)
|
||||||
if (CHANNEL_MATCH_NAME == headerAccessor.destination) {
|
if (CHANNEL_MATCH_NAME == headerAccessor.destination) {
|
||||||
messagingTemplate.convertAndSend(CHANNEL_MATCH_NAME, messageHistory)
|
messagingTemplate.convertAndSend(CHANNEL_MATCH_NAME, messageHistory)
|
||||||
println("Lancement...")
|
println("Lancement du WebSocket")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@MessageMapping("/plusPoint/{playerId}")
|
@MessageMapping("/plusPoint")
|
||||||
fun addPoint(match:MatchBean, @PathVariable playerId:Long) {
|
fun addPoint(matchUpdateBean: MatchUpdateBean) {
|
||||||
when (playerId) {
|
val playerId = matchUpdateBean.playerId
|
||||||
match.player1ID -> match.score1 += 1
|
val match = matchUpdateBean.match
|
||||||
match.player2ID -> match.score2 += 1
|
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@MessageMapping("/minusPoint/{playerId}")
|
@MessageMapping("/minusPoint")
|
||||||
fun minusPoint(match:MatchBean, @PathVariable playerId:Long) {
|
fun minusPoint(matchUpdateBean: MatchUpdateBean) {
|
||||||
when (playerId) {
|
val playerId = matchUpdateBean.playerId
|
||||||
match.player1ID -> if (match.score1 > 0) match.score1 -= 1
|
val match = matchUpdateBean.match
|
||||||
match.player2ID -> if (match.score2 > 0) match.score2 -= 1
|
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")
|
@MessageMapping("/add")
|
||||||
fun addMatchtoMainList(match:MatchBean) {
|
fun addMatchtoMainList(match:MatchBean) {
|
||||||
|
|
@ -104,5 +113,6 @@ class MatchWebSocketController(
|
||||||
messageHistory.add(match)
|
messageHistory.add(match)
|
||||||
broadcastMatchUpdate(match, MatchUpdateMessage.UpdateType.NEW_MATCH)
|
broadcastMatchUpdate(match, MatchUpdateMessage.UpdateType.NEW_MATCH)
|
||||||
mainId = mainId?.plus(1)
|
mainId = mainId?.plus(1)
|
||||||
|
println("Ajout du match $match.id en bdd")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package fr.teamflash.fencerjudgeback.websocket.controllers
|
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.entities.RefereeBean
|
||||||
import fr.teamflash.fencerjudgeback.services.RefereeService
|
import fr.teamflash.fencerjudgeback.services.RefereeService
|
||||||
import fr.teamflash.fencerjudgeback.websocket.models.RefereeUpdateMessage
|
import fr.teamflash.fencerjudgeback.websocket.models.RefereeUpdateMessage
|
||||||
|
|
|
||||||
|
|
@ -29,15 +29,14 @@
|
||||||
<div id="messageArea">Connexion...</div>
|
<div id="messageArea">Connexion...</div>
|
||||||
|
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input type="number" id="matchId" placeholder="Match ID">
|
<input type="number" id="player1Id" placeholder="Joueur 1 ID" value="2">
|
||||||
<input type="number" id="player1Id" placeholder="Joueur 1 ID">
|
<input type="number" id="player2Id" placeholder="Joueur 2 ID" value="2">
|
||||||
<input type="number" id="player2Id" placeholder="Joueur 2 ID">
|
<input type="number" id="refereeId" placeholder="Arbitre ID" value="2">
|
||||||
<input type="number" id="refereeId" placeholder="Arbitre ID">
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input type="number" id="score1" placeholder="Score Joueur 1">
|
<input type="number" id="score1" placeholder="Score Joueur 1" value="2">
|
||||||
<input type="number" id="score2" placeholder="Score Joueur 2">
|
<input type="number" id="score2" placeholder="Score Joueur 2" value="14">
|
||||||
<input type="datetime-local" id="matchDate">
|
<input type="datetime-local" id="matchDate">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -63,7 +62,7 @@
|
||||||
|
|
||||||
function sendMatch() {
|
function sendMatch() {
|
||||||
const match = {
|
const match = {
|
||||||
matchId: parseInt(document.getElementById("matchId").value),
|
id: 1,
|
||||||
player1Id: parseInt(document.getElementById("player1Id").value),
|
player1Id: parseInt(document.getElementById("player1Id").value),
|
||||||
player2Id: parseInt(document.getElementById("player2Id").value),
|
player2Id: parseInt(document.getElementById("player2Id").value),
|
||||||
refereeId: parseInt(document.getElementById("refereeId").value),
|
refereeId: parseInt(document.getElementById("refereeId").value),
|
||||||
|
|
@ -73,12 +72,14 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
stompClient.send("/ws/matches/add", {}, JSON.stringify(match));
|
stompClient.send("/ws/matches/add", {}, JSON.stringify(match));
|
||||||
|
|
||||||
|
displayMessage(match)
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayMessage(match) {
|
function displayMessage(match) {
|
||||||
const area = document.getElementById("messageArea");
|
const area = document.getElementById("messageArea");
|
||||||
const div = document.createElement("div");
|
const div = document.createElement("div");
|
||||||
div.textContent = `Match ${match.matchId}: ${match.player1Id} (${match.score1}) vs ${match.player2Id} (${match.score2})`;
|
div.textContent = `Match ${match.id}: ${match.player1Id} (${match.score1}) vs ${match.player2Id} (${match.score2})`;
|
||||||
area.appendChild(div);
|
area.appendChild(div);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -29,15 +29,14 @@
|
||||||
<div id="messageArea">Connexion...</div>
|
<div id="messageArea">Connexion...</div>
|
||||||
|
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input type="number" id="matchId" placeholder="Match ID">
|
<input type="number" id="player1Id" placeholder="Joueur 1 ID" value="2">
|
||||||
<input type="number" id="player1Id" placeholder="Joueur 1 ID">
|
<input type="number" id="player2Id" placeholder="Joueur 2 ID" value="2">
|
||||||
<input type="number" id="player2Id" placeholder="Joueur 2 ID">
|
<input type="number" id="refereeId" placeholder="Arbitre ID" value="2">
|
||||||
<input type="number" id="refereeId" placeholder="Arbitre ID">
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input type="number" id="score1" placeholder="Score Joueur 1">
|
<input type="number" id="score1" placeholder="Score Joueur 1" value="2">
|
||||||
<input type="number" id="score2" placeholder="Score Joueur 2">
|
<input type="number" id="score2" placeholder="Score Joueur 2" value="14">
|
||||||
<input type="datetime-local" id="matchDate">
|
<input type="datetime-local" id="matchDate">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -47,8 +46,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/referees-app'));
|
const stompClient = Stomp.over(new SockJS('/ws/matches-app'));
|
||||||
const channel = "/ws/topic/referee";
|
const channel = "/ws/topic/match";
|
||||||
|
|
||||||
stompClient.connect({}, function () {
|
stompClient.connect({}, function () {
|
||||||
stompClient.subscribe(channel, function (message) {
|
stompClient.subscribe(channel, function (message) {
|
||||||
|
|
@ -63,7 +62,7 @@
|
||||||
|
|
||||||
function sendMatch() {
|
function sendMatch() {
|
||||||
const match = {
|
const match = {
|
||||||
matchId: parseInt(document.getElementById("matchId").value),
|
id: 1,
|
||||||
player1Id: parseInt(document.getElementById("player1Id").value),
|
player1Id: parseInt(document.getElementById("player1Id").value),
|
||||||
player2Id: parseInt(document.getElementById("player2Id").value),
|
player2Id: parseInt(document.getElementById("player2Id").value),
|
||||||
refereeId: parseInt(document.getElementById("refereeId").value),
|
refereeId: parseInt(document.getElementById("refereeId").value),
|
||||||
|
|
@ -72,13 +71,18 @@
|
||||||
date: document.getElementById("matchDate").value
|
date: document.getElementById("matchDate").value
|
||||||
};
|
};
|
||||||
|
|
||||||
stompClient.send("/ws/matches/add", {}, JSON.stringify(match));
|
stompClient.send("/ws/matches/addPoint", {}, JSON.stringify({
|
||||||
|
match: match,
|
||||||
|
playerId: match.player1Id
|
||||||
|
}));
|
||||||
|
|
||||||
|
displayMessage(match)
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayMessage(match) {
|
function displayMessage(match) {
|
||||||
const area = document.getElementById("messageArea");
|
const area = document.getElementById("messageArea");
|
||||||
const div = document.createElement("div");
|
const div = document.createElement("div");
|
||||||
div.textContent = `Match ${match.matchId}: ${match.player1Id} (${match.score1}) vs ${match.player2Id} (${match.score2})`;
|
div.textContent = `Match ${match.id}: ${match.player1Id} (${match.score1}) vs ${match.player2Id} (${match.score2})`;
|
||||||
area.appendChild(div);
|
area.appendChild(div);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
Loading…
Add table
Reference in a new issue