springboot+vue實(shí)現(xiàn)websocket配置過(guò)程解析
1.引入依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <version>1.3.5.RELEASE</version></dependency>
2.配置ServerEndpointExporter
@Configurationpublic class WebSocketConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); }}
這個(gè)bean會(huì)自動(dòng)注冊(cè)使用了@ServerEndpoint注解聲明的Websocket endpoint。
3.創(chuàng)建websocket的ServerEndpoint端點(diǎn)
@Component@ServerEndpoint('/socket')public class WebSocketServer { /** * 全部在線會(huì)話 */ private static Map<String, Session> onlineSessions = new ConcurrentHashMap<>(); /** * 當(dāng)客戶(hù)端打開(kāi)連接:1.添加會(huì)話對(duì)象 2.更新在線人數(shù) */ @OnOpen public void onOpen(Session session) { onlineSessions.put(session.getId(), session); } /** * 當(dāng)客戶(hù)端發(fā)送消息:1.獲取它的用戶(hù)名和消息 2.發(fā)送消息給所有人 * <p> * PS: 這里約定傳遞的消息為JSON字符串 方便傳遞更多參數(shù)! */ @OnMessage public void onMessage(Session session, String jsonStr) { } /** * 當(dāng)關(guān)閉連接:1.移除會(huì)話對(duì)象 2.更新在線人數(shù) */ @OnClose public void onClose(Session session) { onlineSessions.remove(session.getId()); } /** * 當(dāng)通信發(fā)生異常:打印錯(cuò)誤日志 */ @OnError public void onError(Session session, Throwable error) { error.printStackTrace(); } /** * 公共方法:發(fā)送信息給所有人 */ public void sendMessageToAll(String jsonMsg) { onlineSessions.forEach((id, session) -> { try {session.getBasicRemote().sendText(jsonMsg); } catch (IOException e) {e.printStackTrace(); } }); }}
4.前端配置連接與接收消息
<script>import Vue from ’vue’import ElementUI from ’element-ui’;import ’element-ui/lib/theme-chalk/index.css’; // 默認(rèn)主題Vue.use(ElementUI, {size: ’small’});import ’@/directives/directives.js’import Locale from ’@/mixins/locale’;import { t } from ’@/locale’; /** * WebSocket客戶(hù)端 * * 使用說(shuō)明: * 1、WebSocket客戶(hù)端通過(guò)回調(diào)函數(shù)來(lái)接收服務(wù)端消息。例如:webSocket.onmessage * 2、WebSocket客戶(hù)端通過(guò)send方法來(lái)發(fā)送消息給服務(wù)端。例如:webSocket.send(); */ function getWebSocket() { /** * WebSocket客戶(hù)端 PS:URL開(kāi)頭表示W(wǎng)ebSocket協(xié)議 中間是域名端口 結(jié)尾是服務(wù)端映射地址 */ var webSocket = new WebSocket(’ws://10.10.10.3:9117/socket’);//建立與服務(wù)端的連接 /** * 當(dāng)服務(wù)端打開(kāi)連接 */ webSocket.onopen = function (event) { console.log(’WebSocket打開(kāi)連接’); }; /** * 當(dāng)服務(wù)端發(fā)來(lái)消息:1.廣播消息 2.更新在線人數(shù) */ webSocket.onmessage = function (event) { console.log(event) console.log(’WebSocket收到消息:%c’ + event.data, ’color:green’); //獲取服務(wù)端消息 var message = JSON.parse(event.data) || {}; console.log(message)// }; /** * 關(guān)閉連接 */ webSocket.onclose = function (event) { console.log(’WebSocket關(guān)閉連接’); }; /** * 通信失敗 */ webSocket.onerror = function (event) { console.log(’WebSocket發(fā)生異常’); }; return webSocket; } var webSocket = getWebSocket(); /** * 通過(guò)WebSocket對(duì)象發(fā)送消息給服務(wù)端 * 此處沒(méi)有主動(dòng)發(fā)消息給服務(wù)端,如果調(diào)用此方法,則會(huì)發(fā)送消息至socket服務(wù)端onMessage()方法上 */ function sendMsgToServer() { var $message = $(’#msg’); if ($message.val()) { webSocket.send(JSON.stringify({username: $(’#username’).text(), msg: $message.val()})); $message.val(null); } }export default { }
5.實(shí)現(xiàn)后端推送消息至瀏覽器端
@Autowired private WebSocketServer webSocketServer;//注入socket服務(wù) @Override public PuStatesResult queryPuStateUseInfo(QueryCondition condition) { String sql = condition.generatorSql(); if(sql == null){ return null; } List<PuState> puStateList = null; puStateList = puStateMapper.getPusBySql(sql); if(puStateList == null || puStateList.size() == 0){ return null; } PuStatesResult result = new PuStatesResult(); result.setPuStateList(puStateList); result.computePuStates(); if(false == condition.isWithDetail()){ result.setPuStateList(null); } //todo Gson gson = new Gson(); String json = gson.toJson(result); webSocketServer.sendMessageToAll(json);//調(diào)用第3節(jié)中的方法 return result; }
此處是前端查詢(xún)時(shí),服務(wù)器將一些數(shù)據(jù)發(fā)送至前端,假如我們?cè)趯?shí)現(xiàn)有些前端數(shù)據(jù)顯示的時(shí)候,當(dāng)查詢(xún)一次后,如果沒(méi)有斷開(kāi)連接,希望后端數(shù)據(jù)更新后,前端能接收最近數(shù)據(jù)。那么可以在數(shù)據(jù)更新接口中,調(diào)用自己實(shí)現(xiàn)的socket服務(wù)端邏輯,推送消息至服務(wù)端,然后由服務(wù)端監(jiān)聽(tīng),并更新數(shù)據(jù)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. html中的form不提交(排除)某些input 原創(chuàng)3. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼4. 網(wǎng)頁(yè)中img圖片使用css實(shí)現(xiàn)等比例自動(dòng)縮放不變形(代碼已測(cè)試)5. CSS3中Transition屬性詳解以及示例分享6. python 如何在 Matplotlib 中繪制垂直線7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼9. jsp文件下載功能實(shí)現(xiàn)代碼10. 開(kāi)發(fā)效率翻倍的Web API使用技巧
