ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
> Stomp是一种简单(流)文本定向消息协议,提供了一个可互操作的链接格式。允许stomp客户端与任意stomp消息代理(Broker)进行交互。STOMP协议由于设计简单,易于开发客户端,因此在多种语言和多种平台上得到广泛地应用。 * 添加依赖 ~~~ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ~~~ * 配置类 ~~~ /** * @author Exrickx */ @Configuration @EnableWebSocketMessageBroker public class WebSocketStompConfig implements WebSocketMessageBrokerConfigurer { /** * 注册stomp端点 * @param registry */ @Override public void registerStompEndpoints(StompEndpointRegistry registry) { // 允许使用socketJs方式访问 即可通过http://IP:PORT/ws来和服务端websocket连接 registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS(); } /** * 配置信息代理 * @param registry */ @Override public void configureMessageBroker(MessageBrokerRegistry registry) { // 订阅Broker名称 user点对点 topic广播即群发 registry.enableSimpleBroker("/user","/topic"); // 全局(客户端)使用的消息前缀 registry.setApplicationDestinationPrefixes("/app"); // 点对点使用的前缀 无需配置 默认/user registry.setUserDestinationPrefix("/user"); } } ~~~ 复制 * 由于只做广播和点对点的消息推送,这里只用到订阅发布 ~~~ @Autowired private SimpMessagingTemplate messagingTemplate; // 广播 messagingTemplate.convertAndSend("/topic/subscribe", "您收到了新的系统消息"); // 通过用户ID实现点对点 messagingTemplate.convertAndSendToUser(id,"/queue/subscribe", "您收到了新的消息"); ~~~