码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Spring STOMP @SubscribeMapping 的使用


    使用方法

    标记在方法上,用于将客户端以 “/app” 开头的订阅路由到对应的方法上。默认情况下,返回值会通过clientOutboundChannel直接返回(发送)给客户端,不经过broker,是一次性的。用户可以通过 @SendTo 或 @SendToUser 去修改这一行为,也就是将返回值发送个broker,然后广播出去。

    方法参数

    Method argumentDescription
    MessageFor access to the complete message.
    MessageHeadersFor access to the headers within the Message.
    MessageHeaderAccessor, SimpMessageHeaderAccessor, and StompHeaderAccessorFor access to the headers through typed accessor methods.
    @PayloadFor access to the payload of the message, converted (for example, from JSON) by a configured MessageConverter.


    The presence of this annotation is not required since it is, by default, assumed if no other argument is matched.


    You can annotate payload arguments with @javax.validation.Valid or Spring’s @Validated, to have the payload arguments be automatically validated.

    @HeaderFor access to a specific header value — along with type conversion using an org.springframework.core.convert.converter.Converter, if necessary.
    @HeadersFor access to all headers in the message. This argument must be assignable to java.util.Map.
    @DestinationVariableFor access to template variables extracted from the message destination. Values are converted to the declared method argument type as necessary.
    java.security.PrincipalReflects the user logged in at the time of the WebSocket HTTP handshake.

    用途

    一般用于初始化数据。如登入聊天室后,初始化在线人员列表和历史消息等。不是真正的订阅。

    代码示例:

    服务端:

    registry.enableStompBrokerRelay("/topic");
    registry.setApplicationDestinationPrefixes("/app");
    
    @SubscribeMapping("/chatroom")
    public List<String> chatInit(){
        List<String> onlineList = new ArrayList<>();
        onlineList.add("Kleven");
        onlineList.add("Dean");
        return onlineList;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    客户端:

    stompClient.subscribe('/app/chatroom', (msg) => {
        // client logic
    });
    
    • 1
    • 2
    • 3

    结果:

    >>> SUBSCRIBE
    id:sub-1
    destination:/app/chatroom
    
    <<< MESSAGE
    destination:/app/chatroom
    content-type:application/json
    subscription:sub-1
    message-id:e1vkwn0p-0
    content-length:17
    
    ["Kleven","Dean"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    官方文档

    https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#websocket-stomp-subscribe-mapping

  • 相关阅读:
    Java项目:springboot+vue电影院会员管理系统
    B_QuRT_User_Guide(33)
    基于Boost的搜索引擎
    域名列入备案黑名单解除教程
    Python基于OpenCV的交通路口红绿灯控制系统设计
    Python实操如何去除EXCEL表格中的公式并保留原有的数值
    Python安装Jnius库报错DLL load failed:找不到模块
    系统学习Python——类(class)代码的编写基础与实例:类通过继承进行定制
    2.1 八大类50条小红书爆款标题文案【玩赚小红书】
    代码随想录训练营第38天|理论基础 、LeetCode 509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯
  • 原文地址:https://blog.csdn.net/u012359704/article/details/125628194
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号