【Spring源码】WebSocket做推送动作的底层实例

一、前瞻

Ok,开始我们今天的对Spring的【模块阅读】。

在这里插入图片描述

那就挑Web里的WebSocket模块,先思考下本次阅读的阅读线索

  1. WebSocket在Spring里起到什么作用
  2. 这个模块采用了什么设计模式
  3. 我们都知道WebSocket可以主动推送消息给用户,那做推送动作的底层实例究竟是谁

二、探索

我们先整体看下整个模块的组织结构。

在这里插入图片描述

要解决阅读线索3,看起来应该从上图里的server模块开始。

找了一圈发现并没有什么线索,那我们就从阅读官方文档这个方向入手。

在这里插入图片描述

官方文档表面心跳采用了SckJS协议,所以我们应该从图2里的sockjs模块开始探索。

既然是发送消息给用户,那必定有send的动作,我们在sockjs模块全局搜索下。

在这里插入图片描述

我们根据搜索定位到了发送消息的WebSocketSession接口

	/**
	 * Send a WebSocket message: either {@link TextMessage} or {@link BinaryMessage}.
	 * <p><strong>Note:</strong> The underlying standard WebSocket session (JSR-356) does
	 * not allow concurrent sending. Therefore, sending must be synchronized. To ensure
	 * that, one option is to wrap the {@code WebSocketSession} with the
	 * {@link org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator
	 * ConcurrentWebSocketSessionDecorator}.
	 * @see org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator
	 */
	void sendMessage(WebSocketMessage<?> message) throws IOException;

整体看下该接口的类图,看下是哪些具体的子类对象实现了sendMessage接口。

在这里插入图片描述

我们找到了以上链条的sendMessage具体实现:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值