做企业邮箱的网站,谷歌推广费用多少,wordpress 文章表格,清远东莞网站建设spring框架对websocket有很好的支持#xff0c;stomp协议作为websocket的子协议#xff0c;Spring也做了很多封装#xff0c;让我们在开发中易于使用。 学习使用Spring的Websocket模块#xff0c;当然最好的办法就是看官网说明了。本篇文章对官网做一些简述和个人的理解。 …spring框架对websocket有很好的支持stomp协议作为websocket的子协议Spring也做了很多封装让我们在开发中易于使用。 学习使用Spring的Websocket模块当然最好的办法就是看官网说明了。本篇文章对官网做一些简述和个人的理解。
开始使用
依赖引入
第一步当然是引入SpringBoot的包了
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-websocket/artifactId
/dependency这个包实际上引入了两个依赖spring-messaging 和 spring-websocket
如何开启stomp的支持
Configuration
EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {Overridepublic void registerStompEndpoints(StompEndpointRegistry registry) {registry.addEndpoint(/portfolio); }Overridepublic void configureMessageBroker(MessageBrokerRegistry config) {config.setApplicationDestinationPrefixes(/app); config.enableSimpleBroker(/topic, /queue); }
}registry.addEndpoint(“/portfolio”); 添加一个后端前端可以通过这个端点进行websocket通信 对应的前端代码可以这么写 var socket new SockJS(/portfolio);stompClient Stomp.over(socket);config.setApplicationDestinationPrefixes(“/app”); 这个是前端可以往这个路径发送消息。 前端代码这么写的 stompClient.send(/app/echo, {}, JSON.stringify(msg));后端可以定义一个controller来接收这个消息所以这个/app的意思可以理解为所有MessageMapping的前缀。
Controller
RequiredArgsConstructor(onConstructor_ {Autowired})
public class StompController {MessageMapping(/echo)public void echo(Principal principal, Msg msg) {//代码略}
}config.enableSimpleBroker(“/topic”, “/queue”); 这个是启用消息broker。广播消息的前缀。当我们需要发送广播消息给客户端时需要满足这个前缀条件。 前端这么订阅消息是topic前缀 //订阅广播消息topicstompClient.subscribe(/topic/boardCast/hello, function (response) {});后端代码通过消息broker可以将此消息发送给订阅了/topic/boardCast/hello的客户端。
public class StompController {private final SimpMessageSendingOperations msgOperations;public void test(message) {msgOperations.send(/topic/boardCast/hello,message);}
}消息的流转过程
首先得理解以下几个概念
Message: 消息包括消息头和消息体.MessageHandler: 处理消息的处理器MessageChannel:消息通道客户端发送消息到达服务器(inboundChannel)。服务器通过通道(outboundChannel)发送消息给客户端MessageBroker:消息分发的处理器消息怎么流转是由broker分发的 代码示例 前端往/app/echo发送了一条消息 //主动发送消息给服务器对应的后端topic为/app/echo2function send() {var value document.getElementById(content).value;var msg {msgType: 1,content: value};stompClient.send(/app/echo2, {}, JSON.stringify(msg));}后端代码得注册/app前缀
Configuration
EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {Overridepublic void configureMessageBroker(MessageBrokerRegistry config) {config.setApplicationDestinationPrefixes(/app); config.enableSimpleBroker(/topic, /queue); }
}然后定义一个Controller来接收用户消息 MessageMapping(“/echo”)这里就是子路径了拼起来正好是/app/echo这时Wesocket请求会到达echo方法。
Controller
RequiredArgsConstructor(onConstructor_ {Autowired})
public class StompController {private final SimpMessageSendingOperations msgOperations;MessageMapping(/echo2)public void echo2() {msgOperations.convertAndSend(/topic/boardCast/hello, hello boardCast Message);}
}上面示例代码的执行流程消息的流转如下图所示
消息通过inboundChannel到服务器此时根据消息的前缀会匹配出/app开头的是需要找SimpAnnotationMethodMessageHandler。这个处理器是找Controller来执行Controller中收到该消息其方法中调用了一个发送方法。发往/topic/boardCast/hello此时也会根据消息的前缀找到消息处理器SimpleBrokerMessageHandlerSimpleBrokerMessageHandler遍历用户会话找到订阅了/topic/boardCast/hello的用户。通过outboundChannel将消息发送出去
以上就是用户发送一个消息服务端接收。服务端同时再发送一条广播消息给对应的客户端的过程。
总结
通过本节内容我们学到了以下内容
几个配置的含义 registry.addEndpoint(“/portfolio”); 配置WebSocket端点config.setApplicationDestinationPrefixes(“/app”); 配置Controller的目的前缀。这是用于服务端接收客户端消息的前缀config.enableSimpleBroker(“/topic”, “/queue”); 配置用户可以订阅的destination。服务端通过msgOperations.convertAndSend(“/topic/boardCast/hello”, “hello boardCast Message”);可以发送消息给订阅了此destination的用户 消息的流转过程大家可以根据上面的流程图阅读一下源码
本节的示例源码都在开源项目中文章链接【stomp实战】搭建一套websocket推送平台。文章最后有项目地址。