New message endpoint chat
This commit is contained in:
parent
4a7e68dd4f
commit
66a1799ff8
@ -0,0 +1,4 @@
|
||||
package com.example.cezenBTC.DTO.race_card;
|
||||
|
||||
public record StopBets(boolean status, int raceNumber) {
|
||||
}
|
||||
@ -112,7 +112,8 @@ public class CezenRoutsSecurityChain {
|
||||
"/cezen/set_aors",
|
||||
"/cezen/set_password",
|
||||
"/cezen/add_extension",
|
||||
"/test/crash"
|
||||
"/test/crash",
|
||||
"/test/stop-bets"
|
||||
).hasAnyRole("admin")
|
||||
//any one who is authenticated can access /logout
|
||||
.requestMatchers("/user/getXSRfToken","/user/ping", "/logout").authenticated()
|
||||
|
||||
@ -13,14 +13,15 @@ public class CezenWebSocketConfig implements WebSocketMessageBrokerConfigurer{
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry config) {
|
||||
|
||||
config.enableSimpleBroker("/topic"); // Enables a simple memory-based message broker to carry messages back to the client on destinations prefixed with /topic
|
||||
config.setApplicationDestinationPrefixes("/app"); // Prefix for messages from client to backend
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
registry.addEndpoint("/websocket").setAllowedOrigins("*"); // Native WebSocket
|
||||
|
||||
registry.addEndpoint("/websocket").setAllowedOrigins("*"); // Native WebSocket
|
||||
registry.addEndpoint("/websocket").setAllowedOrigins("*").withSockJS(); // Registers "/websocket" endpoint and enables SockJS fallback
|
||||
}
|
||||
|
||||
|
||||
@ -2,9 +2,7 @@ package com.example.cezenBTC.controller;
|
||||
|
||||
import com.example.cezenBTC.service.BtcWebSocketService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/test")
|
||||
@ -14,10 +12,19 @@ public class TestApi {
|
||||
BtcWebSocketService btcWebSocketService;
|
||||
|
||||
@GetMapping("/crash")
|
||||
String breakConnection(){
|
||||
public String breakConnection(){
|
||||
|
||||
btcWebSocketService.sendServerDownNotification();
|
||||
|
||||
return "Backend error";
|
||||
}
|
||||
|
||||
// Stop bets for a particular race
|
||||
@GetMapping("/stop-bets")
|
||||
public String stopBets(@RequestParam int raceNumber) {
|
||||
|
||||
btcWebSocketService.sendStatusMessage(raceNumber);
|
||||
return "Bets stopped for race " + raceNumber;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
package com.example.cezenBTC.controller;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
|
||||
@Controller
|
||||
public class WebSocketLoggingController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WebSocketLoggingController.class);
|
||||
|
||||
// Listen for messages sent to destination /app/log (based on your config)
|
||||
@MessageMapping("/log")
|
||||
public void logIncomingMessage(@Payload String message) {
|
||||
logger.info("Received WebSocket message: {}", message);
|
||||
// You can add more processing here if needed
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package com.example.cezenBTC.service;
|
||||
|
||||
import com.example.cezenBTC.DTO.race_card.StopBets;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -10,6 +11,13 @@ public class BtcWebSocketService {
|
||||
@Autowired
|
||||
private SimpMessagingTemplate template;
|
||||
|
||||
// Sends a structured status message object on /topic/status
|
||||
public void sendStatusMessage(final int raceNumber) {
|
||||
StopBets statusMessage = new StopBets(true, raceNumber);
|
||||
template.convertAndSend("/topic/status", statusMessage);
|
||||
}
|
||||
|
||||
|
||||
public void sendServerDownNotification() {
|
||||
template.convertAndSend("/topic/server-status", "DOWN");
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user