1

cannot establish connection to my websocket server by browser client.

configuration:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
 config.enableSimpleBroker("/topic/", "/queue/");
 config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
 registry.addEndpoint("/greeting");
}
}

And controller:

@MessageMapping("/message")
@SendToUser("/queue/reply")
public String processMessageFromClient(@Payload String message, Principal principal) throws Exception {
 String name = new Gson().fromJson(message, Map.class).get("name").toString();
 System.out.println(name);
 //messagingTemplate.convertAndSendToUser(principal.getName(), "/queue/reply", name);
 return name;
}

I start server, and then open index.html in browser then make connect to ws://localhost:8080/greeting and after that sending message to /app/message

but actually happens nothing. Browser inspector shows 404. What's wronng i do?

asked Sep 23, 2020 at 5:51
1
  • Use @MessageMapping("/queue/reply") instead of @SendToUser("/queue/reply") as above as below answer. Commented Sep 23, 2020 at 6:19

1 Answer 1

1

Here is the way that use to implement WebSocket in Spring. First, you should configure the Web socket message broker and register the stomp endpoint as below. Here I use setAllowedOrigins("*").withSockJS() to access this endpoint to any host.

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
 @Override
 public void registerStompEndpoints(StompEndpointRegistry registry) {
 registry.addEndpoint("/greeting")
 .setAllowedOrigins("*")
 .withSockJS();
 }
 @Override
 public void configureMessageBroker(MessageBrokerRegistry registry) {
 registry.setApplicationDestinationPrefixes("/app")
 .enableSimpleBroker("/topic", "/queue/");
 }
}

Then I create the controller as below.

@Controller
public class WebSocketController {
 private final SimpMessagingTemplate template;
 @Autowired
 WebSocketController(SimpMessagingTemplate template){
 this.template = template;
 }
 @MessageMapping("/queue/reply")
 public void sendMessage(String message){
 System.out.println(message);
 this.template.convertAndSend("/topic", message);
 }
}
 

Use @MessageMapping("/queue/reply") instead of @SendToUser("/queue/reply") as above.

From that Simple Messaging Template, I used convertAndSend() method to asynchronous data communication with that message broker. If there is any data comes to that message broker it will automatically send that data using the above configured endpoint called /socket with SockJS and Stomp.

You can refer this article to learn more about the Spring web socket.

answered Sep 23, 2020 at 6:10
Sign up to request clarification or add additional context in comments.

2 Comments

that doesn't work. also tested via websocketking.com Can it be that OS blocks ws somehow?
Did you found the solution? I have exact same problem

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.