初次使用 WebSocket -springboot 集成
参考自:SpringBoot 集成websocket_清泉影月的博客-CSDN博客,WebSocket中利用service层交互数据库_戒烟的李白的博客-CSDN博客_websocketservice
核心依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <version>2.7.0</version> </dependency>
<!--用于把字符串转json 可不配置-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
配置类
import com.deloitte.WebsocketServer.WebSocketServer; import com.deloitte.mapper.system.SysUsersMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter endpointExporter() { return new ServerEndpointExporter(); }
//用来配置交互数据库 直接使用@Autowired 空指针 @Autowired private void SysUsersMapper(SysUsersMapper sysUsersMapper){ WebSocketServer.sysUsersMapper=sysUsersMapper; } }
服务
@Component @Service @ServerEndpoint("/websocket/{clientId}") public class WebSocketServer { //静态变量,用来记录当前在线连接数 private static int onlineCount = 0; //用来存放每个客户端对应的的连接信息 private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<>(); //与某个客户端的连接会话,需要通过它来给客户端发送数据 private Session session; //连接的 clientId private String clientId = ""; /** * 前端连接建立成功调用的方法 */ @OnOpen public void onOpen(Session session, @PathParam("clientId") String clientId) { this.session = session; this.clientId = clientId; webSocketSet.add(this); addOnlineCount(); try { String msg = "clientId:" + clientId + "连接成功,当前在线客户端数:" + getOnlineCount(); sendMessage(msg); System.err.println(msg); } catch (IOException e) { System.err.println("client:{},连接故障,原因:{}"+ clientId+ e.getMessage()); } } /** * 前端连接关闭调用的方法 * 可以在里面写一些关闭连接需要处理的逻辑 */ @OnClose public void onClose() { webSocketSet.remove(this); subOnlineCount(); System.err.println("释放连接,clientId:{};当前连接数:{}"+ clientId+getOnlineCount()); } public static SysUsersMapper sysUsersMapper; //换成自己的 /** * 该方法用于接受客户端发送的消息 * 当前逻辑是:相同 clientId 的连接可以接受消息 */ @OnMessage public void onMessage(String message, Session session) { // System.err.println("收到来自clientId:{} 的消息:{}"+clientId+ message); Gson gson = new Gson(); Map<String, Object> map = new HashMap<String, Object>(); map = gson.fromJson(message, map.getClass()); System.err.println(map.get("frozen")); String s=map.get("frozen")+""; sysUsersMapper.savelog(s,s,s,s, s, s); for (WebSocketServer item : webSocketSet) { try { if (item.clientId.equals(clientId)) { //广播发送的话就去掉if item.sendMessage(message); } } catch (IOException e) { System.err.println("client:{} 发送消息故障,原因:{}"+ item.clientId+ e.getMessage()); } } } /** * 用于记录发生的错误 */ @OnError public void onError(Session session, Throwable e) { System.err.println("出现故障:{}"+e.getMessage()); } /** * 服务器主动推送消息 */ public void sendMessage(String message) throws IOException { this.session.getBasicRemote().sendText(message); } /** * 定向 clientId 发送消息 */ public void sendInfo(String clientId, String message) throws IOException { System.err.println("推送消息到clientId:{},msg:{}"+clientId+message); for (WebSocketServer item : webSocketSet) { if (item.clientId.equals(clientId)) { item.sendMessage(message); } } } /** * 群发消息,广播形式 */ public void sendInfoAll(String message) throws IOException { for (WebSocketServer item : webSocketSet) { item.sendMessage(message); } } public static synchronized int getOnlineCount() { return onlineCount; } public static synchronized void addOnlineCount() { WebSocketServer.onlineCount++; } public static synchronized void subOnlineCount() { WebSocketServer.onlineCount--; } public static CopyOnWriteArraySet<WebSocketServer> getWebSocketSet() { return webSocketSet; } }