日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

java getbasicremote_Vue+Java 通过websocket实现服务器与客户端双向通信操作

發布時間:2025/3/19 vue 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java getbasicremote_Vue+Java 通过websocket实现服务器与客户端双向通信操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. vue代碼

methods: {

//在方法里調用 this.websocketsend()發送數據給服務器

onConfirm () {

//需要傳輸的數據

let data = {

code: 1,

item: ‘傳輸的數據'

}

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 () { // 連接建立之后執行send方法發送數據

let data = {

code: 0,

msg: '這是client:初次連接'

}

this.websocketsend(JSON.stringify(data))

},

websocketonerror () {

console.log( 'WebSocket連接失敗')

},

websocketonmessage (e) { // 數據接收

console.log('數據接收' + e.data)

},

websocketsend (Data) { // 數據發送

this.websock.send(Data)

},

websocketclose (e) { // 關閉

console.log('已關閉連接', e)

}

},

created () {

console.log('created')

this.initWebSocket()

},

data () {

return {

websocket: null

}

},

destroyed () {

this.websock.close() // 離開路由之后斷開websocket連接

}

2. java代碼

項目引入tomcat安裝目錄里的兩個依賴包

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 clients = new ConcurrentHashMap();

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"));

// 發送數據給服務端

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 getClients() {

return clients;

}

}

在項目別的類可通過new WebSocket()向客戶端發送數據

WebSocket ws = new WebSocket();

ws.sendMessageAll(JSON.toJSONString(rs));

以上這篇Vue+Java 通過websocket實現服務器與客戶端雙向通信操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的java getbasicremote_Vue+Java 通过websocket实现服务器与客户端双向通信操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。