简体   繁体   中英

spring websocket cannot establish connection

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM