简单介绍
WebSocket是一个持久化的协议,它是伴随HTTP5而出的协议,用来解决HTTP不支持持久化连接的问题,算是对http协议的补充。
优点:
1.可以在浏览器中使用
2.支持双向通信 实时性强
3.使用简单
和Http的关系(如图所示)
两者有交集,但不存在包含关系。
WebSocket借用了HTTP协议来完成一部分握手。只需要一次HTTP握手(程序设计中叫回调)。一次请求,持久连接,无需反复鉴别用户,服务端主动PUSH。
实体
/**
* @ClassName: MsgPO
* @Desc: 消息实体
* @Author: ysq
* @Date: 2021-07-23 12:33:47
*/
@Data
@ApiModel(description = "消息记录实体")
public class MessageVO {
@ApiModelProperty(name = "tenantId", value = "租户id", example = "c2d831d21e32427188130f202602b787", required = true)
private String tenantId;
@ApiModelProperty(name = "businessId", value = "业务主键", example = "66bda848890a41d1b558a17ad681365a", required = true)
private String businessId;
@ApiModelProperty(name = "recordType", value = "消息类型", example = "0", required = true)
private MsgTypeEnum recordType;
@ApiModelProperty(name = "recordTitle", value = "消息标题", example = "小心翼翼!消防员抱被救婴儿有多温柔", required = true)
private String recordTitle;
@ApiModelProperty(name = "recordContent", value = "消息内容", example = "{\"id\":\"1\",\"name\":\"张三\"}", required = true)
private String recordContent;
}
Controller
@RequestMapping(value = "/sys/")
public class MsgNoticeApiController implements MsgNoticeApi {
@Autowired
private MsgNoticeService msgNoticeService;
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;
@GlobalTransactional
@Transactional
@Override
public int insertSelective(MsgNotice record) {
int number = msgNoticeService.insertSelective(record);
if (number > 0) {
simpMessagingTemplate.convertAndSend(WebSocketConfig.NOTICE, record);
}
return number;
}
@Override
public void sendMessage(MessageVO record) {
simpMessagingTemplate.convertAndSend(WebSocketConfig.NOTICE_BUSINESS, record);
}
@Override
public void sendJsonMessage(String tenantId, String jsonObject) {
simpMessagingTemplate.convertAndSend(WebSocketConfig.NOTICE_BUSINESS, jsonObject);
}
@Override
public void newMessage(String tenantId) {
simpMessagingTemplate.convertAndSend(WebSocketConfig.NOTICE_BUSINESS, true);
}
}
Config
spring mvc的controller层的annotation是RequestMapping大家都知道,同样的,WebSocket也有同样功能的annotation,就是MessageMapping,其值就是访问地址。
@EnableWebSocketMessageBroker
@Configuration
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
final public static String P2P = "/admin/p2p";
final public static String NOTICE = "/admin/notice";
final public static String NOTICE_BUSINESS = "/admin/notice_business";
// 添加这个Endpoint,这样在网页中就可以通过websocket连接上服务,也就是我们配置websocket的服务地址,并且可以指定是否使用socketjs
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/admin/websocket").setAllowedOrigins("*").withSockJS();
registry.addEndpoint("/admin/websocket").setAllowedOrigins("*");
}
// 配置消息代理,哪种路径的消息会进行代理处理
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
//点对点,公告,业务通知
registry.enableSimpleBroker(P2P, NOTICE, NOTICE_BUSINESS);
}
/**
* 配置客户端入站通道拦截器
*/
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.setInterceptors(createUserInterceptor());
}
/**
* @return
* @Title: createUserInterceptor
* @Description: 将客户端渠道拦截器加入spring ioc容器
*/
@Bean
public UserInterceptor createUserInterceptor() {
return new UserInterceptor();
}
}