簡體   English   中英

Spring Websocket:如何攔截@MessageMapping回復中的返回類型

[英]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) {
 ...
}

觀察NotificationNotificationReply類型。

到這里為止,所有工作如何預期

我有一個擴展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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM