Spring Websocket:如何从@MessageMapping 回复中拦截 return 类型

Spring Websocket: how intercept the return type from the @MessageMapping reply

我有一个 Spring 使用 websocket 的应用程序

我可以进行连接和发送消息等

@Controller是:

@MessageMapping("/ws/notification")
@SendTo("/topic/springframework.topic.websocket.reply")
public NotificationReply handleNotification(Notification notification) {
 ...
}

观察 NotificationNotificationReply 类型。

到这里为止一切正常

我有一个 class 扩展 ChannelInterceptorAdapter 并使用方法:

我能够以某种方式检索 Notification 类型。

更多详情见:

但现在考虑return类型。

在做了研究之后,我确实意识到 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在'server'之下。因此,解决方案是为 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,您可以执行以下操作: