Hello everyone I'm having some issues figuring out some websocket endpoints. I am trying to build a basic chat application to help me gain an understanding of websockets.
I'm running a Java 8 Spring Boot Websocket server that is running on http://localhost:8080 for testing purposes.
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketMessageConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(final StompEndpointRegistry registry) {
WebSocketMessageBrokerConfigurer.super.registerStompEndpoints(registry);
registry.addEndpoint("/websocket").setAllowedOriginPatterns("*").withSockJS();
}
@Override
public void configureMessageBroker(final MessageBrokerRegistry registry) {
WebSocketMessageBrokerConfigurer.super.configureMessageBroker(registry);
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/topic");
}
}
@Controller
public class SocketController {
@MessageMapping("/socket.send")
@SendTo("/topic/public")
public SocketMessage sendMessage(@Payload final SocketMessage message) {
return message;
}
@MessageMapping("/socket.newUser")
@SendTo("/topic/public")
public SocketMessage newUser(@Payload final SocketMessage message,
SimpMessageHeaderAccessor headerAccessor) {
headerAccessor.getSessionAttributes().put("username", message.getSender());
return message;
}
}
That's my config file and controller file... My understanding is that the registerStompEndpoints() is giving the endpoint to setup the connection between client/server for a session.
The front end request will look like
That seems to work fine from my javascript front end request. I see a good log showing a connection to that, but when I try to send a newUser or a message, neither appear to be properly going to the server. I don't see anything in the logs so I suspect my endpoints from the front-end are incorrect...
const socket = new SockJS('http://localhost:8080/websocket')
stompClient.subscribe('http://localhost:8080/topic/public', onMessageReceived)
stompClient.send("http://localhost:8080/app/socket.newUser",
stompClient.send("http://localhost:8080/app/socket.send", {}, JSON.stringify(chatMessage))
This is my console.log from the from the front end
>>> SUBSCRIBE
id:sub-0
destination:http://localhost:8080/app/topic/public
>>> SEND
destination:http://localhost:8080/app/socket.newUser
content-length:36
{"sender":"testname","type":"CONNECT"}