Spring?Boot+Vue實現Socket通知推送的完整步驟
Spring Boot端
第一步,引入依賴
首先我們需要引入WebSocket所需的依賴,以及處理輸出格式的依賴
<!--格式轉換-->
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<!--WebSocket依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>第二步,創(chuàng)建WebSocket配置類
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @author: tjp
* @create: 2023-04-03 09:58
* @Description: WebSocket配置
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}第三步,創(chuàng)建WebSocket服務
這一步我們通過userId作為標識符,區(qū)分系統(tǒng)中對應的用戶,后續(xù)也可基于此,進行其他的操作步驟。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.excel.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
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.HashMap;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author: tjp
* @create: 2023-04-03 13:55
* @Description: WebSocket服務
*/
@ServerEndpoint("/websocket/{userId}")
@Slf4j
@Component
public class WebSocketServer {
/**
* 靜態(tài)變量,用來記錄當前在線連接數。應該把它設計成線程安全的。
*/
private static int onlineCount = 0;
/**
* concurrent包的線程安全Set,用來存放每個客戶端對應的MyWebSocket對象。
*/
private static ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
/**
* 與某個客戶端的連接會話,需要通過它來給客戶端發(fā)送數據
*/
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中
} else {
webSocketMap.put(userId, this);
//加入set中
addOnlineCount();
//在線數加1
}
log.info("用戶連接:" + userId + ",當前在線人數為:" + getOnlineCount());
try {
HashMap<Object, Object> map = new HashMap<>();
map.put("key", "連接成功");
sendMessage(JSON.toJSONString(map));
} catch (IOException e) {
log.error("用戶:" + userId + ",網絡異常!!!!!!");
}
}
/**
* 連接關閉調用的方法
*/
@OnClose
public void onClose() {
if (webSocketMap.containsKey(userId)) {
webSocketMap.remove(userId);
//從set中刪除
subOnlineCount();
}
log.info("用戶退出:" + userId + ",當前在線人數為:" + getOnlineCount());
}
/**
* 收到客戶端消息后調用的方法
*
* @param message 客戶端發(fā)送過來的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
log.info("用戶消息:" + userId + ",報文:" + message);
//可以群發(fā)消息
//消息保存到數據庫、redis
if (StringUtils.isNotBlank(message)) {
try {
//解析發(fā)送的報文
JSONObject jsonObject = JSONObject.parseObject(message);
//追加發(fā)送人(防止串改)
jsonObject.put("fromUserId", this.userId);
String fromUserId = jsonObject.getString("fromUserId");
//傳送給對應toUserId用戶的websocket
if (StringUtils.isNotBlank(fromUserId) && webSocketMap.containsKey(fromUserId)) {
webSocketMap.get(fromUserId).sendMessage(jsonObject.toJSONString());
//自定義-業(yè)務處理
// DeviceLocalThread.paramData.put(jsonObject.getString("group"),jsonObject.toJSONString());
} else {
log.error("請求的userId:" + fromUserId + "不在該服務器上");
//否則不在這個服務器上,發(fā)送到mysql或者redis
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 發(fā)生錯誤時候
*
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("用戶錯誤:" + this.userId + ",原因:" + error.getMessage());
error.printStackTrace();
}
/**
* 實現服務器主動推送
*/
public void sendMessage(String message) throws IOException {
//加入線程鎖
synchronized (session) {
try {
//同步發(fā)送信息
this.session.getBasicRemote().sendText(message);
} catch (IOException e) {
log.error("服務器推送失敗:" + e.getMessage());
}
}
}
/**
* 發(fā)送自定義消息
* */
/**
* 發(fā)送自定義消息
*
* @param message 發(fā)送的信息
* @param toUserId 如果為null默認發(fā)送所有
* @throws IOException
*/
public static void sendInfo(String message, String toUserId) throws IOException {
//如果userId為空,向所有群體發(fā)送
if (StringUtils.isEmpty(toUserId)) {
//向所有用戶發(fā)送信息
Iterator<String> itera = webSocketMap.keySet().iterator();
while (itera.hasNext()) {
String keys = itera.next();
WebSocketServer item = webSocketMap.get(keys);
item.sendMessage(message);
}
}
//如果不為空,則發(fā)送指定用戶信息
else if (webSocketMap.containsKey(toUserId)) {
WebSocketServer item = webSocketMap.get(toUserId);
item.sendMessage(message);
} else {
log.error("請求的userId:" + toUserId + "不在該服務器上");
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
public static synchronized ConcurrentHashMap<String, WebSocketServer> getWebSocketMap() {
return WebSocketServer.webSocketMap;
}
}第四步,創(chuàng)建Controller進行發(fā)送測試
獲取當前在線人數
import com.......WebSocketServer;
@ApiOperation(value = "獲取當前在線人數")
@GetMapping("/getOnlineCount")
public Integer getOnlineCount() {
return WebSocketServer.getOnlineCount();
}通過接口,向前端用戶推送消息
import com.......WebSocketServer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
/**
* @author: tjp
* @create: 2023-04-03 13:57
* @Description: 測試
*/
@RestController
@RequestMapping("/news")
public class NewsController {
@GetMapping("/send")
public String send() {
try {
WebSocketServer.sendInfo("這是websocket發(fā)送過來的消息!", "需要推送的用戶的編號");
} catch (IOException e) {
throw new RuntimeException(e);
}
return "發(fā)送消息成功";
}
}Vue端
第一步,創(chuàng)建連接工具類
創(chuàng)建工具類websocket.js,這里的userId就是用來作為標識符的userId
/**
* @author: tjp
* @create: 2023-04-03 11:22
* @Description: Socket客戶端
*/
export class WebSocketClient {
constructor(userId) {
this.userId = userId;
this.websocket = null;
this.timeout = 10000; // 心跳超時時間,單位ms
this.timeoutObj = null; // 心跳定時器
this.serverTimeoutObj = null; // 服務器超時定時器
this.lockReconnect = false; // 避免重復連接
this.timeoutnum = null; // 重連延遲定時器
}
// 初始化WebSocket連接
initWebSocket() {
let wsUrl = `ws://127.0.0.1:8080/websocket/${this.userId}`;
this.websocket = new WebSocket(wsUrl);
this.websocket.onopen = this.websocketonopen.bind(this);
this.websocket.onerror = this.websocketonerror.bind(this);
this.websocket.onmessage = this.setOnmessageMessage.bind(this);
this.websocket.onclose = this.websocketclose.bind(this);
// 監(jiān)聽窗口關閉事件,當窗口關閉時,主動去關閉websocket連接,防止連接還沒斷開就關閉窗口,server端會拋異常。
window.onbeforeunload = this.websocketclose.bind(this);
}
// 啟動心跳
start() {
console.log('start');
// 清除延時器
this.timeoutObj && clearTimeout(this.timeoutObj);
this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
/*// 向服務器發(fā)送心跳消息
let actions = { "test": "12345" };
this.websocket && this.websocket.readyState == 1 && this.websocket.send(JSON.stringify(actions));
// 啟動心跳定時器
this.timeoutObj = setTimeout(() => {
this.start();
// 定義一個延時器等待服務器響應,若超時,則關閉連接,重新請求server建立socket連接
this.serverTimeoutObj = setTimeout(() => {
this.websocket.close();
}, this.timeout)
}, this.timeout)*/
}
// 重置心跳
reset() {
// 清除時間
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
// 重啟心跳
this.start();
}
// 重新連接
reconnect() {
if (this.lockReconnect) return;
this.lockReconnect = true;
// 沒連接上會一直重連,設置延遲避免請求過多
this.timeoutnum && clearTimeout(this.timeoutnum);
this.timeoutnum = setTimeout(() => {
this.initWebSocket();
this.lockReconnect = false;
}, 5000)
}
// 處理收到的消息
async setOnmessageMessage(event) {
console.log(event.data, '獲得消息');
// 重置心跳
// this.reset();
// 自定義全局監(jiān)聽事件
window.dispatchEvent(new CustomEvent('onmessageWS', {
detail: {
data: event.data
}
}))
// //發(fā)現消息進入 開始處理前端觸發(fā)邏輯
// if (event.data === 'success' || event.data === 'heartBath') return
}
// WebSocket連接成功回調
websocketonopen() {
// 開啟心跳
this.start();
console.log("WebSocket連接成功!!!" + new Date() + "----" + this.websocket.readyState);
clearInterval(this.otimer);//停止
}
// WebSocket連接錯誤回調
websocketonerror(e) {
console.log("WebSocket連接發(fā)生錯誤" + e);
}
// WebSocket連接關閉回調
websocketclose(e) {
this.websocket.close();
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
console.log("websocketcloe關閉連接")
}
// 關閉WebSocket連接
closeWebSocket() {
this.websocket.close();
console.log("closeWebSocket關閉連接")
}
// 監(jiān)聽窗口關閉事件
onbeforeunload() {
this.closeWebSocket();
}
}第二步,建立連接
在任意你想建立連接的頁面中建立Socket連接
比如,在用戶點擊登錄按鈕之后
在這里可以使用原型,創(chuàng)建連接對象,并啟動連接
<script>
import Vue from "vue";
import {WebSocketClient} from "@/utils/websocket";
......
......
methods:{
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true
this.$store.dispatch('user/login', this.loginForm).then(() => {
this.$router.push({path: this.redirect || '/'})
this.loading = false
/*-----------在此處放入原型中------------*/
Vue.prototype.$WebSocketClientInstance = new WebSocketClient('t');
Vue.prototype.$WebSocketClientInstance.initWebSocket()
/*-----------------end------------*/
}).catch(() => {
this.loading = false
})
} else {
this.$message({message: '請?zhí)顚懻_格式的用戶名或密碼', type: 'error'})
return false
}
})
}
}
.....
.....
</script>

第三步,監(jiān)聽服務器發(fā)送過來的消息
在你想監(jiān)聽的頁面,使用監(jiān)聽器進行監(jiān)聽
<script>
....
....
mounted() {
// 添加socket通知監(jiān)聽
window.addEventListener('onmessageWS', this.getSocketData)
},
methods: {
// 收到消息處理
getSocketData(res) {
console.log(res.detail)
console.log("llll")
},
}
....
....
</script>這個時候,你就可以通過后端的接口進行發(fā)送了
搞個測試


第四步,關閉連接
搞個按鈕
<template>
<div>
<button @click="closeConnect">關閉連接</button>
</div>
</template>
<script>
import {WebSocketClient} from "@/utils/websocket";
import Vue from "vue";
export default {
methods: {
closeConnect() {
console.dir(Vue.prototype)
Vue.prototype.$WebSocketClientInstance.closeWebSocket();
},
}
}
</script>
總結
到此這篇關于Spring Boot+Vue實現Socket通知推送的文章就介紹到這了,更多相關SpringBoot Vue實現Socket通知推送內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring中的FactoryBean與BeanFactory詳細解析
這篇文章主要介紹了Spring中的FactoryBean與BeanFactory詳細解析,在Spring框架中,FactoryBean和BeanFactory是兩個關鍵的接口,用于創(chuàng)建和管理對象實例,它們在Spring的IoC(Inversion of Control,控制反轉)容器中發(fā)揮著重要的作用,需要的朋友可以參考下2023-11-11
java web開發(fā)中大量數據導出Excel超時(504)問題解決
開發(fā)測試時候導入數據遇到大數據導入的問題,整理了下,需要的朋友可以參考下2017-04-04

