问题描述
我正在使用@EnableWebSocketMessageBroker
创建简单的STOMP代理.当@MessageMapping
方法中发生RuntimeException
时,我想接收STOMP ERROR
帧,但是默认情况下它不起作用.
I am creating simple STOMP broker using @EnableWebSocketMessageBroker
. When RuntimeException
happens in @MessageMapping
method I would like to receive STOMP ERROR
frame, but it doesn't work like that by default.
似乎简单代理中不支持ERROR
框架: https://github.com/spring-projects/spring-framework/blob/master/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageType.java# L28-L44
It seems that ERROR
frame is not supported in simple broker:https://github.com/spring-projects/spring-framework/blob/master/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageType.java#L28-L44
另一方面,有一种机制可以发送spring-websocket
中已经可用的ERROR
帧,并且其代码引用了一些与简单代理相关的类: https://github.com/spring-projects/spring-framework/blob/master/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler. java#L349
On the other hand, there is a mechanism to send ERROR
frames already available in spring-websocket
and its code references some classes which are related to simple broker:https://github.com/spring-projects/spring-framework/blob/master/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java#L349
如何启用在@MessageMapping
方法中发生RuntimeException
时发送ERROR
帧的机制?
How to enable mechanism of sending ERROR
frames when RuntimeException
happens in @MessageMapping
method?
推荐答案
执行此操作的快速简便的方法是,在对@MessageMapping端点进行任何调用之前,先在客户端上分配特定的错误处理程序订阅.然后,使用try catch块将对方法的所有调用包装在您的websocket服务器中,并处理发生的异常.然后是做类似这样的简单情况:
Quick and easy way to do this is to have a specific error handler subscription on the client that subsribes before making any calls to the @MessageMapping endpoints. Then wrap all calls to the method in your websocket server with a try catch block and handle the exceptions that occur. Then its a simple case of doing something like:
messagingTemplate.convertAndSend( "/topic/clientControl/1234", "SHUT_DOWN"); (or send to user etc, depends on your subscription model)
您可以向客户端发送错误消息并允许其处理,也可以决定在服务器端执行什么操作并进行控制订阅,以允许服务器将命令传递给客户端.
You can either send an error message to the client and allow it to handle it or decide what to do server side and have a control subscription allowing the server to pass commands to the client.
这篇关于使用STOMP简单代理时的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!