Spring boot + Vue整合 Websocket实现消息推送
WebSocket
1.什么是webSocket WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。
2.webSocket可以做什么? 1.在线股票网站。 2.即时聊天。 3.多人在线游戏。 4.系统性能实时监控。 …
3.spring boot整合
3.1 maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
3.2 WebSocket配置类
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; /** * WebSocket的配置信息 */ @Configuration public class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
3.3 WebSocket操作类 通过该类WebSocket可以进行群推送以及单点推送
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.util.concurrent.ConcurrentHashMap; @Component @Slf4j @ServerEndpoint("/api/pushMessage/{userId}") public class WebSocketServer { /** * 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 */ private static int onlineCount = 0; /** * concurrent包的线程安全Set,用来存放每个客户端对应的WebSocket对象。 */ private static final ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>(); /** * 与某个客户端的连接会话,需要通过它来给客户端发送数据 */ private Session session; /** * 接收userId */ private String userId = ""; /** * 连接建立成 * 功调用的方法 */ @OnOpen public void onOpen(Session session, @PathParam("userId") String userId) { this.session = session; this.userId = userId; if (webSocketMap.containsKey(userId)) { webSocketMap.remove(userId); //加入set中 webSocketMap.put(userId, this); } else { //加入set中 webSocketMap.put(userId, this); //在线数加1 addOnlineCount(); } log.info("用户连接:" + userId + ",当前在线人数为:" + getOnlineCount()); // sendMessage("连接成功"); } /** * 连接关闭 * 调用的方法 */ @OnClose public void onClose() { if (webSocketMap.containsKey(userId)) { webSocketMap.remove(userId); //从set中删除 subOnlineCount(); } log.info("用户退出:" + userId + ",当前在线人数为:" + getOnlineCount()); } /** * 收到客户端消 * 息后调用的方法 * * @param message 客户端发送过来的消息 **/ @OnMessage public void onMessage(String message, Session session) { log.info("用户消息:" + userId + ",报文:" + message); //可以群发消息 //消息保存到数据库、redis if (StringUtils.isNotBlank(message)) { try { //解析发送的报文 JSONObject jsonObject = JSON.parseObject(message); //追加发送人(防止串改) jsonObject.put("fromUserId", this.userId); String toUserId = jsonObject.getString("toUserId"); //传送给对应toUserId用户的websocket if (StringUtils.isNotBlank(toUserId) && webSocketMap.containsKey(toUserId)) { webSocketMap.get(toUserId).sendMessage(message); } else { //否则不在这个服务器上,发送到mysql或者redis log.error("请求的userId:" + toUserId + "不在该服务器上"); } } catch (Exception e) { e.printStackTrace(); } } } /** * @param session Session * @param error Throwable */ @OnError public void onError(Session session, Throwable error) { log.error("用户错误:" + this.userId + ",原因:" + error.getMessage()); error.printStackTrace(); } /** * 实现服务 * 器主动推送 */ public void sendMessage(String message) { try { this.session.getBasicRemote().sendText(message); } catch (IOException e) { e.printStackTrace(); } } /** * 发送自定 * 义消息 **/ public static void sendInfo(String message, String userId) { log.info("发送消息到:" + userId + ",报文:" + message); if (StringUtils.isNotBlank(userId) && webSocketMap.containsKey(userId)) { webSocketMap.get(userId).sendMessage(message); } else { log.error("用户" + userId + ",不在线!"); } } /** * 获得此时的 * 在线人数 * * @return int */ public static synchronized int getOnlineCount() { return onlineCount; } /** * 在线人 * 数加1 */ public static synchronized void addOnlineCount() { WebSocketServer.onlineCount++; } /** * 在线人 * 数减1 */ public static synchronized void subOnlineCount() { WebSocketServer.onlineCount--; } }
- Vue部分
<script> export default { data () { return { websock: null } }, created () { this.initWebSocket() }, methods: { initWebSocket () { const wsuri = ws://localhost:8080/api/pushMessage/1 this.websock = new WebSocket(wsuri) this.websock.onmessage = this.websocketOnMessage this.websock.onopen = this.websocketOnOpen this.websock.onerror = this.websocketOnError this.websock.onclose = this.websocketClose }, // websocket连接后发送数据(send发送) websocketOnOpen () { console.log(websock已打开) }, // 连接建立失败重连 websocketOnError () { this.initWebSocket() }, // 数据接收 websocketOnMessage (e) { debugger console.log(this.websock.readyState) this.$message.error(e.data) console.log(e.data) this.websocketClose() }, // 数据发送 websocketSend (Data) { this.websock.send(Data) }, // 关闭 websocketClose (e) { console.log(断开连接, e) } } } </script>
上一篇:
多线程四大经典案例