![](/img/trans.png)
[英]How to access request object in @MessageMapping method with spring websocket implementation
[英]Spring Websocket: how intercept the return type from the @MessageMapping reply
我有一個使用websocket的Spring App
我可以進行連接並發送消息等。
@Controller
是:
@MessageMapping("/ws/notification")
@SendTo("/topic/springframework.topic.websocket.reply")
public NotificationReply handleNotification(Notification notification) {
...
}
觀察Notification
和NotificationReply
類型。
到這里為止,所有工作如何預期
我有一個擴展ChannelInterceptorAdapter
並使用該方法的類:
public void postSend(Message<?> message, MessageChannel channel, boolean sent)
我能夠以某種方式檢索Notification
類型。
更多詳細信息,請參見:
但是現在考慮返回類型。
NotificationReply
類型? 經過研究后,我意識到StompCommand
枚舉具有以下內容:
public enum StompCommand {
// client
STOMP(SimpMessageType.CONNECT),
CONNECT(SimpMessageType.CONNECT),
DISCONNECT(SimpMessageType.DISCONNECT),
SUBSCRIBE(SimpMessageType.SUBSCRIBE, true, true, false),
UNSUBSCRIBE(SimpMessageType.UNSUBSCRIBE, false, true, false),
SEND(SimpMessageType.MESSAGE, true, false, true),
ACK(SimpMessageType.OTHER),
NACK(SimpMessageType.OTHER),
BEGIN(SimpMessageType.OTHER),
COMMIT(SimpMessageType.OTHER),
ABORT(SimpMessageType.OTHER),
// server
CONNECTED(SimpMessageType.OTHER),
RECEIPT(SimpMessageType.OTHER),
MESSAGE(SimpMessageType.MESSAGE, true, true, true),
ERROR(SimpMessageType.OTHER, false, false, true);
...
因此,請注意, MESSAGE
在“服務器”下。 因此,解決方案是為MESSAGE
添加一個case
,如下所示:
@Override
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
StompHeaderAccessor stompHeaderAccessor = StompHeaderAccessor.wrap(message);
....
switch(stompHeaderAccessor.getCommand()) {
case CONNECT:
logger.info("postSend ...");
logger.info("STOMP Connect");
break;
...
case MESSAGE:
logger.info("postSend ...");
logger.info("STOMP MESSAGE");
.... do more
break;
...
case SEND:
logger.info("postSend ...");
logger.info("STOMP Send");
printDetails(message, stompHeaderAccessor);
break;
...
}
如果要檢查Message<?>
的payload
,可以執行以下操作:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.