Spring?Boot+Vue實現(xiàn)Socket通知推送的完整步驟
Spring Boot端
第一步,引入依賴
首先我們需要引入WebSocket所需的依賴,以及處理輸出格式的依賴
<!--格式轉(zhuǎn)換-->
<!-- 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服務(wù)
這一步我們通過userId作為標(biāo)識符,區(qū)分系統(tǒng)中對應(yīng)的用戶,后續(xù)也可基于此,進(jìn)行其他的操作步驟。
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服務(wù)
*/
@ServerEndpoint("/websocket/{userId}")
@Slf4j
@Component
public class WebSocketServer {
/**
* 靜態(tài)變量,用來記錄當(dāng)前在線連接數(shù)。應(yīng)該把它設(shè)計成線程安全的。
*/
private static int onlineCount = 0;
/**
* concurrent包的線程安全Set,用來存放每個客戶端對應(yīng)的MyWebSocket對象。
*/
private static ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
/**
* 與某個客戶端的連接會話,需要通過它來給客戶端發(fā)送數(shù)據(jù)
*/
private Session session;
/**
* 接收userId
*/
private String userId = "";
/**
* 連接建立成功調(diào)用的方法
*/
@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();
//在線數(shù)加1
}
log.info("用戶連接:" + userId + ",當(dāng)前在線人數(shù)為:" + getOnlineCount());
try {
HashMap<Object, Object> map = new HashMap<>();
map.put("key", "連接成功");
sendMessage(JSON.toJSONString(map));
} catch (IOException e) {
log.error("用戶:" + userId + ",網(wǎng)絡(luò)異常!!!!!!");
}
}
/**
* 連接關(guān)閉調(diào)用的方法
*/
@OnClose
public void onClose() {
if (webSocketMap.containsKey(userId)) {
webSocketMap.remove(userId);
//從set中刪除
subOnlineCount();
}
log.info("用戶退出:" + userId + ",當(dāng)前在線人數(shù)為:" + getOnlineCount());
}
/**
* 收到客戶端消息后調(diào)用的方法
*
* @param message 客戶端發(fā)送過來的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
log.info("用戶消息:" + userId + ",報文:" + message);
//可以群發(fā)消息
//消息保存到數(shù)據(jù)庫、redis
if (StringUtils.isNotBlank(message)) {
try {
//解析發(fā)送的報文
JSONObject jsonObject = JSONObject.parseObject(message);
//追加發(fā)送人(防止串改)
jsonObject.put("fromUserId", this.userId);
String fromUserId = jsonObject.getString("fromUserId");
//傳送給對應(yīng)toUserId用戶的websocket
if (StringUtils.isNotBlank(fromUserId) && webSocketMap.containsKey(fromUserId)) {
webSocketMap.get(fromUserId).sendMessage(jsonObject.toJSONString());
//自定義-業(yè)務(wù)處理
// DeviceLocalThread.paramData.put(jsonObject.getString("group"),jsonObject.toJSONString());
} else {
log.error("請求的userId:" + fromUserId + "不在該服務(wù)器上");
//否則不在這個服務(wù)器上,發(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();
}
/**
* 實現(xiàn)服務(wù)器主動推送
*/
public void sendMessage(String message) throws IOException {
//加入線程鎖
synchronized (session) {
try {
//同步發(fā)送信息
this.session.getBasicRemote().sendText(message);
} catch (IOException e) {
log.error("服務(wù)器推送失敗:" + e.getMessage());
}
}
}
/**
* 發(fā)送自定義消息
* */
/**
* 發(fā)送自定義消息
*
* @param message 發(fā)送的信息
* @param toUserId 如果為null默認(rèn)發(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 + "不在該服務(wù)器上");
}
}
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進(jìn)行發(fā)送測試
獲取當(dāng)前在線人數(shù)
import com.......WebSocketServer;
@ApiOperation(value = "獲取當(dāng)前在線人數(shù)")
@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就是用來作為標(biāo)識符的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; // 服務(wù)器超時定時器
this.lockReconnect = false; // 避免重復(fù)連接
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)聽窗口關(guān)閉事件,當(dāng)窗口關(guān)閉時,主動去關(guān)閉websocket連接,防止連接還沒斷開就關(guān)閉窗口,server端會拋異常。
window.onbeforeunload = this.websocketclose.bind(this);
}
// 啟動心跳
start() {
console.log('start');
// 清除延時器
this.timeoutObj && clearTimeout(this.timeoutObj);
this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
/*// 向服務(wù)器發(fā)送心跳消息
let actions = { "test": "12345" };
this.websocket && this.websocket.readyState == 1 && this.websocket.send(JSON.stringify(actions));
// 啟動心跳定時器
this.timeoutObj = setTimeout(() => {
this.start();
// 定義一個延時器等待服務(wù)器響應(yīng),若超時,則關(guān)閉連接,重新請求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;
// 沒連接上會一直重連,設(shè)置延遲避免請求過多
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ā)現(xiàn)消息進(jìn)入 開始處理前端觸發(fā)邏輯
// if (event.data === 'success' || event.data === 'heartBath') return
}
// WebSocket連接成功回調(diào)
websocketonopen() {
// 開啟心跳
this.start();
console.log("WebSocket連接成功!!!" + new Date() + "----" + this.websocket.readyState);
clearInterval(this.otimer);//停止
}
// WebSocket連接錯誤回調(diào)
websocketonerror(e) {
console.log("WebSocket連接發(fā)生錯誤" + e);
}
// WebSocket連接關(guān)閉回調(diào)
websocketclose(e) {
this.websocket.close();
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
console.log("websocketcloe關(guān)閉連接")
}
// 關(guān)閉WebSocket連接
closeWebSocket() {
this.websocket.close();
console.log("closeWebSocket關(guān)閉連接")
}
// 監(jiān)聽窗口關(guā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)聽服務(wù)器發(fā)送過來的消息
在你想監(jiān)聽的頁面,使用監(jiān)聽器進(jìn)行監(jiān)聽
<script>
....
....
mounted() {
// 添加socket通知監(jiān)聽
window.addEventListener('onmessageWS', this.getSocketData)
},
methods: {
// 收到消息處理
getSocketData(res) {
console.log(res.detail)
console.log("llll")
},
}
....
....
</script>這個時候,你就可以通過后端的接口進(jìn)行發(fā)送了
搞個測試


第四步,關(guān)閉連接
搞個按鈕
<template>
<div>
<button @click="closeConnect">關(guān)閉連接</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>
總結(jié)
到此這篇關(guān)于Spring Boot+Vue實現(xiàn)Socket通知推送的文章就介紹到這了,更多相關(guān)SpringBoot Vue實現(xiàn)Socket通知推送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jersey Restful接口如何獲取參數(shù)的問題
這篇文章主要介紹了Jersey Restful接口如何獲取參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
常用的Java數(shù)據(jù)結(jié)構(gòu)知識點匯總
這篇文章主要介紹了常用的Java數(shù)據(jù)結(jié)構(gòu)知識點匯總,數(shù)據(jù)結(jié)構(gòu)分線性數(shù)據(jù)結(jié)構(gòu)和非線性數(shù)據(jù)結(jié)構(gòu),下面對此作詳細(xì)介紹,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)或工作有所幫助2022-03-03
Java?Excel?Poi字體顏色自定義設(shè)置代碼
最近項目使用POI按模板導(dǎo)出Excel,需要設(shè)置單元格的字體為紅色,下面這篇文章主要給大家介紹了關(guān)于Java?Excel?Poi字體顏色自定義設(shè)置的相關(guān)資料,需要的朋友可以參考下2024-01-01
Spring中的FactoryBean與BeanFactory詳細(xì)解析
這篇文章主要介紹了Spring中的FactoryBean與BeanFactory詳細(xì)解析,在Spring框架中,FactoryBean和BeanFactory是兩個關(guān)鍵的接口,用于創(chuàng)建和管理對象實例,它們在Spring的IoC(Inversion of Control,控制反轉(zhuǎn))容器中發(fā)揮著重要的作用,需要的朋友可以參考下2023-11-11
java web開發(fā)中大量數(shù)據(jù)導(dǎo)出Excel超時(504)問題解決
開發(fā)測試時候?qū)霐?shù)據(jù)遇到大數(shù)據(jù)導(dǎo)入的問題,整理了下,需要的朋友可以參考下2017-04-04

