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

那就挑Web里的WebSocket模块,先思考下本次阅读的阅读线索:
- WebSocket在Spring里起到什么作用
- 这个模块采用了什么设计模式
- 我们都知道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具体实现:

最低0.47元/天 解锁文章
285

被折叠的 条评论
为什么被折叠?



