简介
本文介绍使用Springboot简单实现Websocket的完整流程。由于是入门教程,所以以一个简单的案例呈现,包括了服务端和客户端的实现,这里服务端使用Springboot搭建,而客户端使用Java来实现。
什么是Websocket
在开始之前,先简单介绍一下Websocket的相关知识。Websocket是应用层协议,传输层使用了TCP协议来实现。Websocket与HTTP协议的不同之处在于,Websocket是全双工的,支持服务器与客户端的双向通信,而HTTP协议只能由客户端向服务端发起请求来获取资源。因此,Websocket的一个常见的应用场景是服务端向多个客户端推送消息。
环境配置
笔者的开发环境配置为:
- AdoptOpenJDK 11
- IntelliJ IDEA 2020.1.2 x
服务端实现
本章节介绍服务端的实现。
1、引入Jar包
首先,在IDEA中创建一个Maven项目,在pom.xml文件中引入所需要的Jar包,如下所示:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
一个是springboot的starter,另一个是springboot内部集成的websocket。
2、Springboot集成的Websocket
使用Springboot集成的websocket需要使用到以下几个类/接口:
除了上述三个官方提供的类/接口之外,我们还需要实现一个WebSocket的包装类,用于为每一个WebSocket实例添加额外的信息,比如客户端的ID。在Spring内,WebSocket实例以WebSocketSession形式存在,每个session都代表了一个服务端与客户端的会话。
2.1、创建WebSocket包装类
import org.springframework.web.socket.WebSocketSession;
public class WebSocketBean {
private WebSocketSession webSocketSession;
private int clientId;
public int getClientId() {
return clientId;
}
public void setClientId(int clientId) {
this.clientId = clientId;
}
public WebSocketSession getWebSocketSession() {
return webSocketSession;
}
public void setWebSocketSession(WebSocketSession webSocketSession) {
this.webSocketSession = webSocketSession;
}
}
这里的包装类很简单,仅添加了一个客户端ID的属性,这里仅作为简单的示例。
2.2、实现WebSocketHandler接口
WebSocketHandler接口用于处理WebSocket的消息,Spring提供了一个抽象类AbstractWebSocketHandler实现了WebSocketHandler接口,因此我们可以直接继承抽象类,重写需要实现的方法即可。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
public class MyWebsocketHandler extends AbstractWebSocketHandler {
private final Logger logger = LoggerFactory.getLogger(getClass());
private static final Map<String, WebSocketBean> webSocketBeanMap;
private