Vue+Java 通過(guò)websocket實(shí)現(xiàn)服務(wù)器與客戶端雙向通信操作
1. vue代碼
methods: { //在方法里調(diào)用 this.websocketsend()發(fā)送數(shù)據(jù)給服務(wù)器 onConfirm () { //需要傳輸?shù)臄?shù)據(jù) let data = { code: 1, item: ‘傳輸?shù)臄?shù)據(jù)’ } this.websocketsend(JSON.stringify(data)) }, /* */ initWebSocket () { // 初始化weosocket let userinfo = getUserInfo() let username = userinfo.waiter_userid this.websock = new WebSocket(’ws://’ + baseURL + ’/websocket/’ + username) this.websock.onmessage = this.websocketonmessage this.websock.onerror = this.websocketonerror this.websock.onopen = this.websocketonopen this.websock.onclose = this.websocketclose }, websocketonopen () { // 連接建立之后執(zhí)行send方法發(fā)送數(shù)據(jù) let data = { code: 0, msg: ’這是client:初次連接’ } this.websocketsend(JSON.stringify(data)) }, websocketonerror () { console.log( ’WebSocket連接失敗’) }, websocketonmessage (e) { // 數(shù)據(jù)接收 console.log(’數(shù)據(jù)接收’ + e.data) }, websocketsend (Data) { // 數(shù)據(jù)發(fā)送 this.websock.send(Data) }, websocketclose (e) { // 關(guān)閉 console.log(’已關(guān)閉連接’, e) } }, created () { console.log(’created’) this.initWebSocket() }, data () { return { websocket: null } }, destroyed () { this.websock.close() // 離開(kāi)路由之后斷開(kāi)websocket連接 }
2. java代碼
項(xiàng)目引入tomcat安裝目錄里的兩個(gè)依賴(lài)包
package diancan.servlet;import java.io.IOException;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import javax.websocket.OnClose;import javax.websocket.OnError;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.PathParam;import javax.websocket.server.ServerEndpoint;@ServerEndpoint('/websocket/{username}')public class WebSocket { private static int onlineCount = 0; private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>(); private Session session; private String username; @OnOpen public void onOpen(@PathParam('username') String username, Session session) throws IOException { this.username = username; this.session = session; addOnlineCount(); clients.put(username, this); System.out.println('已連接' + username); } @OnClose public void onClose() throws IOException { clients.remove(username); subOnlineCount(); } @OnMessage public void onMessage(String message) throws IOException { DataWrapper res = new DataWrapper(); System.out.println('message:' + message); JSONObject req = JSONObject.parseObject(message);// System.out.println('item:' + req.getJSONObject('item'));// System.out.println('item:' + req.getInteger('code')); // 發(fā)送數(shù)據(jù)給服務(wù)端 sendMessageAll(JSON.toJSONString(res)); } @OnError public void onError(Session session, Throwable error) { error.printStackTrace(); } public void sendMessageTo(String message, String To) throws IOException { // session.getBasicRemote().sendText(message); // session.getAsyncRemote().sendText(message); for (WebSocket item : clients.values()) { if (item.username.equals(To)) item.session.getAsyncRemote().sendText(message); } } public void sendMessageAll(String message) throws IOException { for (WebSocket item : clients.values()) { item.session.getAsyncRemote().sendText(message); } } public static synchronized int getOnlineCount() { return onlineCount; } public static synchronized void addOnlineCount() { WebSocket.onlineCount++; } public static synchronized void subOnlineCount() { WebSocket.onlineCount--; } public static synchronized Map<String, WebSocket> getClients() { return clients; }}
在項(xiàng)目別的類(lèi)可通過(guò)new WebSocket()向客戶端發(fā)送數(shù)據(jù)
WebSocket ws = new WebSocket();
ws.sendMessageAll(JSON.toJSONString(rs));
以上這篇Vue+Java 通過(guò)websocket實(shí)現(xiàn)服務(wù)器與客戶端雙向通信操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 改變 Python 中線程執(zhí)行順序的方法2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以?xún)?nèi))3. 詳解Python模塊化編程與裝飾器4. Python下使用Trackbar實(shí)現(xiàn)繪圖板5. html小技巧之td,div標(biāo)簽里內(nèi)容不換行6. Python通過(guò)format函數(shù)格式化顯示值7. python web框架的總結(jié)8. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式9. python使用ctypes庫(kù)調(diào)用DLL動(dòng)態(tài)鏈接庫(kù)10. Python性能測(cè)試工具Locust安裝及使用
