websocket fixing

This commit is contained in:
clemcle81500 2025-06-02 15:30:57 +02:00
parent 4177bf8c52
commit eaefd42f1d
3 changed files with 10 additions and 9 deletions

View file

@ -23,7 +23,7 @@ open class WebSocketConfig : WebSocketMessageBrokerConfigurer {
override fun registerStompEndpoints(registry: StompEndpointRegistry) {
// 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)
.withSockJS()
}

View file

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

View file

@ -47,8 +47,8 @@
<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'));
const channel = "/topic/match.updates";
const stompClient = Stomp.over(new SockJS('/ws/matches-app'));
const channel = "/ws/topic";
stompClient.connect({}, function () {
stompClient.subscribe(channel, function (message) {
@ -72,7 +72,7 @@
date: document.getElementById("matchDate").value
};
stompClient.send("/app/match.update", {}, JSON.stringify(match));
stompClient.send("/ws/matches/add", {}, JSON.stringify(match));
}
function displayMessage(match) {