简体   繁体   English

spring websocket 无法建立连接

[英]spring websocket cannot establish connection

cannot establish connection to my websocket server by browser client.无法通过浏览器客户端与我的 websocket 服务器建立连接。

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:和 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我启动服务器,然后在浏览器中打开 index.html 然后连接到ws://localhost:8080/greeting然后发送消息到/app/message

but actually happens nothing.但实际上什么也没发生。 Browser inspector shows 404. What's wronng i do?浏览器检查器显示 404。我怎么了?

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.这是在 Spring 中实现 WebSocket 的方法。首先,您应该配置 Web 套接字消息代理并注册 stomp 端点,如下所示。 Here I use setAllowedOrigins("*").withSockJS() to access this endpoint to any host.在这里,我使用 setAllowedOrigins("*").withSockJS() 将此端点访问到任何主机。

@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,如下所示。

@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.使用@MessageMapping("/queue/reply") 而不是上面的@SendToUser("/queue/reply")。

From that Simple Messaging Template, I used convertAndSend() method to asynchronous data communication with that message broker.从那个简单的消息传递模板,我使用 convertAndSend() 方法与那个消息代理进行异步数据通信。 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.如果有任何数据到达该消息代理,它将使用上面配置的名为 /socket 的端点与 SockJS 和 Stomp 自动发送该数据。

You can refer this article to learn more about the Spring web socket.您可以参考本文了解更多关于 Spring web 插座的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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