英文:
spring websocket cannot establish connection
问题
无法通过浏览器客户端与我的WebSocket服务器建立连接。
配置:
@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");
}
}
控制器:
@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;
}
我启动了服务器,然后在浏览器中打开index.html,然后连接到ws://localhost:8080/greeting
,然后发送消息到/app/message
,但实际上什么都没有发生。浏览器检查器显示404错误。我做错了什么?
英文:
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?
答案1
得分: 1
以下是使用Spring实现WebSocket的方式。首先,您应该配置WebSocket消息代理并注册stomp端点,如下所示。这里我使用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/");
}
}
然后我创建以下控制器。
@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);
}
}
使用@MessageMapping("/queue/reply")替代上面的@SendToUser("/queue/reply")。
通过这个Simple Messaging Template,我使用convertAndSend()方法与消息代理进行异步数据通信。如果有任何数据传输到消息代理,它将自动使用上面配置的带有SockJS和Stomp的端点/socket来发送该数据。
您可以参考此文章以了解更多关于Spring WebSocket的信息。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论