最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue+Java 通過websocket實(shí)現(xiàn)服務(wù)器與客戶端雙向通信操作

 更新時(shí)間:2020年09月22日 10:48:53   作者:oayoat  
這篇文章主要介紹了Vue+Java 通過websocket實(shí)現(xiàn)服務(wù)器與客戶端雙向通信操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧

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() // 離開路由之后斷開websocket連接
 }

2. java代碼

項(xiàng)目引入tomcat安裝目錄里的兩個(gè)依賴包

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)目別的類可通過new WebSocket()向客戶端發(fā)送數(shù)據(jù)

WebSocket ws = new WebSocket();

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

以上這篇Vue+Java 通過websocket實(shí)現(xiàn)服務(wù)器與客戶端雙向通信操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用vue實(shí)現(xiàn)grid-layout功能實(shí)例代碼

    使用vue實(shí)現(xiàn)grid-layout功能實(shí)例代碼

    這篇文章主要介紹了使用vue實(shí)現(xiàn)grid-layout功能的代碼講解,需要的朋友可以參考下
    2018-01-01
  • vueCl如何查看打包后文件的大小占比

    vueCl如何查看打包后文件的大小占比

    這篇文章主要介紹了vueCl如何 查看打包后文件的大小占比問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Nuxt.js SSR與權(quán)限驗(yàn)證的實(shí)現(xiàn)

    Nuxt.js SSR與權(quán)限驗(yàn)證的實(shí)現(xiàn)

    這篇文章主要介紹了Nuxt.js SSR與權(quán)限驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • vue使用axios?post發(fā)送json數(shù)據(jù)跨域請(qǐng)求403的解決方案

    vue使用axios?post發(fā)送json數(shù)據(jù)跨域請(qǐng)求403的解決方案

    這篇文章主要介紹了vue使用axios?post發(fā)送json數(shù)據(jù)跨域請(qǐng)求403的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • vue單行文本溢出會(huì)出現(xiàn)title提示自定義指令

    vue單行文本溢出會(huì)出現(xiàn)title提示自定義指令

    這篇文章主要為大家介紹了vue單行文本溢出會(huì)出現(xiàn)title提示自定義指令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • vue項(xiàng)目本地開發(fā)使用Nginx配置代理后端接口問題

    vue項(xiàng)目本地開發(fā)使用Nginx配置代理后端接口問題

    這篇文章主要介紹了vue項(xiàng)目本地開發(fā)使用Nginx配置代理后端接口問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Vue中如何引用公共方法和公共組件

    Vue中如何引用公共方法和公共組件

    這篇文章主要介紹了Vue中如何引用公共方法和公共組件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue項(xiàng)目el-upload?上傳文件及回顯照片和下載文件功能實(shí)現(xiàn)

    Vue項(xiàng)目el-upload?上傳文件及回顯照片和下載文件功能實(shí)現(xiàn)

    本次需求是上傳多種固定格式的文件,且回顯的時(shí)候,圖片可以正常顯示,文件可以進(jìn)行下載,主要采用element的el-upload組件實(shí)現(xiàn),對(duì)Vue項(xiàng)目el-upload?上傳文件及回顯照片和下載文件功能實(shí)現(xiàn)感興趣的朋友跟隨小編一起看看吧
    2023-12-12
  • Vue實(shí)現(xiàn)定位并解決內(nèi)存泄漏

    Vue實(shí)現(xiàn)定位并解決內(nèi)存泄漏

    Vue.js?是一個(gè)流行且強(qiáng)大的?JavaScript?框架,它允許我們構(gòu)建動(dòng)態(tài)和交互式?Web?應(yīng)用程序,本文我們將深入探討?Vue.js?應(yīng)用程序中內(nèi)存泄漏的原因,并探索如何定位和修復(fù)這些問題的有效策略,希望對(duì)大家有所幫助
    2023-09-09
  • Vue升級(jí)帶來(lái)的elementui沖突警告:Invalid prop: custom validator check failed for prop “type“.的解決方案

    Vue升級(jí)帶來(lái)的elementui沖突警告:Invalid prop: custom va

    在頁(yè)面渲染的時(shí)候,控制臺(tái)彈出大量警告,嚴(yán)重影響控制臺(tái)的信息獲取功能,但是頁(yè)面基本能正常顯示,這是因?yàn)閂ue升級(jí)帶來(lái)的elementui沖突警告: Invalid prop: custom validator check failed for prop “type“.的解決方案,本文給大家介紹了詳細(xì)的解決方案
    2025-04-04

最新評(píng)論

隆化县| 晋江市| 荣成市| 洛扎县| 延寿县| 西藏| 缙云县| 尼勒克县| 崇文区| 上栗县| 海阳市| 竹山县| 龙游县| 佛学| 广西| 三亚市| 泗阳县| 房山区| 临武县| 洪洞县| 广汉市| 固镇县| 留坝县| 威宁| 鹤壁市| 晋城| 咸宁市| 克拉玛依市| 绩溪县| 安西县| 苍南县| 阳春市| 中西区| 浦东新区| 祥云县| 西乌珠穆沁旗| 那曲县| 满洲里市| 南宁市| 安岳县| 海淀区|