国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue3中使用websocket

這篇具有很好參考價(jià)值的文章主要介紹了vue3中使用websocket。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

效果圖
vue3中使用websocket
實(shí)現(xiàn)
src/util/socket.ts

let websock: any = null;
let global_callback: any = null;
const serverPort = "8080"; // webSocket連接端口
const wsuri = "ws://" + window.location.hostname + ":" + serverPort + "/wsdemo";
function createWebSocket(callback: any) {
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  // @ts-ignore
  if (websock == null || typeof websock !== WebSocket) {
    initWebSocket(callback);
  }
}
function initWebSocket(callback: any) {
  global_callback = callback;
  // 初始化websocket
  websock = new WebSocket(wsuri);
  websock.onmessage = function (e: any) {
    websocketonmessage(e);
  };
  websock.onclose = function (e: any) {
    websocketclose(e);
  };
  websock.onopen = function () {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    websocketOpen();
  };
  // 連接發(fā)生錯(cuò)誤的回調(diào)方法
  websock.onerror = function () {
    console.log("WebSocket連接發(fā)生錯(cuò)誤");
    //createWebSocket();啊,發(fā)現(xiàn)這樣寫會(huì)創(chuàng)建多個(gè)連接,加延時(shí)也不行
  };
}
// 實(shí)際調(diào)用的方法
function sendSock(agentData: any) {
  if (websock.readyState === websock.OPEN) {
    // 若是ws開啟狀態(tài)
    websocketsend(agentData);
  } else if (websock.readyState === websock.CONNECTING) {
    // 若是 正在開啟狀態(tài),則等待1s后重新調(diào)用
    setTimeout(function () {
      sendSock(agentData);
    }, 1000);
  } else {
    // 若未開啟 ,則等待1s后重新調(diào)用
    setTimeout(function () {
      sendSock(agentData);
    }, 1000);
  }
}
function closeSock() {
  websock.close();
}
// 數(shù)據(jù)接收
function websocketonmessage(msg: any) {
  // console.log("收到數(shù)據(jù):"+JSON.parse(e.data));
  // console.log("收到數(shù)據(jù):"+msg);
  // global_callback(JSON.parse(msg.data));
  // 收到信息為Blob類型時(shí)
  let result: any = null;
  // debugger
  if (msg.data instanceof Blob) {
    const reader = new FileReader();
    reader.readAsText(msg.data, "UTF-8");
    reader.onload = (e: any) => {
      console.log(e);
      if (typeof reader.result === "string") {
        result = JSON.parse(reader.result);
      }
      //console.log("websocket收到", result);
      global_callback(result);
    };
  } else {
    result = JSON.parse(msg.data);
    //console.log("websocket收到", result);
    global_callback(result);
  }
}
// 數(shù)據(jù)發(fā)送
function websocketsend(agentData: any) {
  console.log("發(fā)送數(shù)據(jù):" + agentData);
  websock.send(agentData);
}
// 關(guān)閉
function websocketclose(e: any) {
  console.log("connection closed (" + e.code + ")");
}
function websocketOpen(e: any) {
  console.log(e);
  console.log("連接打開");
}
export { sendSock, createWebSocket, closeSock };

src/store/webSocket.ts

import { defineStore } from "pinia";

export const webSocketStore = defineStore("webSocket", {
  state: () => ({
    //推送消息
    data: [],
  }),
  getters: {},

  actions: {
    addMsg(val: any) {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      this.data.push(val);
    },
  },
});

這里面放到了登錄成功后在連接websocket
src/viwes/login.vue

<script setup lang="ts">
import { colorStore } from "@/store/color";
import { webSocketStore } from "@/store/webSocket";
import { createWebSocket } from "@/util/socket";
import { useRouter } from "vue-router";
import md5 from "js-md5"; //
import { ref } from "vue";
import loginApi from "@/api/loginApi";
import { ElNotification } from "element-plus";
const color = colorStore();
const webSocket = webSocketStore();
const routers = useRouter();
const username = ref("009999");
const password = ref("0");
const mes = ref();
const global_callback = (msg: any) => {
  console.log("websocket的回調(diào)函數(shù)收到服務(wù)器信息:" + JSON.stringify(msg));
  // console.log("收到服務(wù)器信息:" + msg);
  mes.value = JSON.parse(JSON.stringify(msg));
  webSocket.addMsg(mes);
  ElNotification({
    title: "您有一條新的消息y",
    message: mes.value.key,
    position: "bottom-right",
  });
};
const login = () => {
  let params = {
    staffCode: username.value,
    password: md5(password.value.toString()),
  };
  loginApi.login(params).then((res: any) => {
    if (res) {
      sessionStorage.setItem("organizationCode", res.hospitalCode);
      sessionStorage.setItem("token", res.token);
      sessionStorage.setItem("staffCode", res.staffCode);
      sessionStorage.setItem("staffName", res.name);
      sessionStorage.setItem("islogin", "true");
      sessionStorage.setItem("roleList", JSON.stringify(res.roles));
      sessionStorage.setItem("currectRole", JSON.stringify(res.roles[0]));
      loginApi.queryMenuByRoleCode(res.roles[0].roleCode).then((res: any) => {
        if (res) {
          sessionStorage.setItem("menu", JSON.stringify(res));
          routers.push("/");
        }
      });
      createWebSocket(global_callback);
    }
  });
};
</script>

<template>
  <div class="con" :style="{ '--color': color.color }">
    <div id="box" class="login-container">
      <div class="left-container">
        <div class="title">
          <span>
            <img src="../../assets/hip.png" style="height: 15px" />{{
              $t("base.title")
            }}</span
          >
        </div>
        <div class="input-container">
          <input
            type="text"
            name="username"
            placeholder="用戶名"
            v-model="username"
          />
          <input
            type="password"
            name="password"
            placeholder="密碼"
            v-model="password"
          />
        </div>
        <div class="message-container">
          <span>Copyright ? 2022 | {{ $t("login.GoodWill") }}</span>
        </div>
      </div>
      <div class="right-container">
        <div class="regist-container">
          <span class="regist">{{ $t("login.WelcomeLogin") }}</span>
        </div>
        <div class="action-container" @click="login">
          <span>{{ $t("login.submit") }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.con {
  height: calc(100vh - 230px);
  padding-top: 10%;
  background-image: linear-gradient(
    to bottom right,
    rgb(114, 135, 254),
    var(--color)
  );
}
.login-container {
  width: 600px;
  height: 315px;
  margin: 0 auto;
  border-radius: 15px;
  box-shadow: 0 10px 50px 0px rbg(59, 45, 159);
  background-color: rgb(95, 76, 194);
}
#box {
  // outline: 4px solid #fff;
  position: relative;
  overflow: hidden;
  z-index: 1;
}
#box::before {
  content: "";
  position: absolute;
  background: lightgray;
  width: 500px;
  height: 400px;
  z-index: -2;
  left: 50%;
  top: 50%;
  transform-origin: 0 0;
  animation: rotate 5s infinite linear;
}
#box::after {
  content: "";
  position: absolute;
  background: rgb(95, 76, 194);
  width: calc(600px - 4px);
  height: calc(315px - 4px);
  left: 2px;
  top: 2px;
  border-radius: 15px;
  z-index: -1;
}
@keyframes rotate {
  to {
    transform: rotate(1turn);
  }
}
.left-container {
  display: inline-block;
  width: 330px;
  // height: 315px;
  height: 191px;
  margin-top: 2px;
  border-top-left-radius: 15px;
  border-bottom-left-radius: 15px;
  padding: 60px;
  background-image: linear-gradient(
    to bottom right,
    rgb(118, 76, 163),
    rgb(92, 103, 211)
  );
}
.left-container::before {
  content: "";
  width: 200px;
  height: 200px;
  left: 50%;
  top: 50%;
  z-index: -1;
  background-image: linear-gradient(
    to bottom right,
    rgb(118, 76, 163),
    rgb(92, 103, 211)
  );
}
.title {
  color: #fff;
  font-size: 18px;
  font-weight: 200;
}
.title span {
  border-bottom: 3px solid rgb(237, 221, 22);
}
.input-container {
  padding: 20px 0;
}
input {
  border: 0;
  background: none;
  outline: none;
  color: #fff;
  margin: 20px 0;
  display: block;
  width: 100%;
  padding: 5px 0;
  transition: 0.2s;
  border-bottom: 1px solid rgb(199, 191, 219);
}
input:hover {
  border-bottom-color: #fff;
}
::-webkit-input-placeholder {
  color: rgb(199, 191, 219);
}
.message-container {
  font-size: 14px;
  transition: 0.2s;
  color: rgb(199, 191, 219);
  cursor: pointer;
}
.message-container:hover {
  color: #fff;
}
.right-container {
  width: 145px;
  display: inline-block;
  height: calc(100% - 120px);
  vertical-align: top;
  padding: 60px 0;
}
.regist-container {
  text-align: center;
  color: #fff;
  font-size: 18px;
  font-weight: 200;
}
.regist-container span {
  border-bottom: 3px solid rgb(237, 221, 22);
}
.action-container {
  font-size: 10px;
  color: #fff;
  text-align: center;
  position: relative;
  top: 200px;
}
.action-container span {
  border: 1px solid rgb(237, 221, 22);
  padding: 10px;
  display: inline;
  line-height: 20px;
  border-radius: 20px;
  position: absolute;
  bottom: 10px;
  left: calc(72px - 20px);
  transition: 0.2s;
  cursor: pointer;
}
.action-container span:hover {
  background-color: rgb(237, 221, 22);
  color: rgb(95, 76, 194);
}
</style>

附贈(zèng)后臺(tái)建議websocket服務(wù)供測(cè)試使用
鏈接:https://pan.baidu.com/s/1RzbWiooLwCIuDTnEfN_x0Q?pwd=p58w
提取碼:p58w文章來源地址http://www.zghlxwxcb.cn/news/detail-508643.html

到了這里,關(guān)于vue3中使用websocket的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 小程序自定義底部導(dǎo)航 custom-tab-bar完整實(shí)現(xiàn)代碼附效果圖

    小程序自定義底部導(dǎo)航 custom-tab-bar完整實(shí)現(xiàn)代碼附效果圖

    根據(jù)用戶身份,動(dòng)態(tài)設(shè)置底部的導(dǎo)航圖標(biāo) ?實(shí)現(xiàn)步驟: 第一步 ,先配置:app.json里面的 tabBar 的 custom 設(shè)置為 true,如圖:這里需要注意的是,自定義 tabBar 中包含的頁面,在這里的 list 頁面路徑也必須得有,其它字段可以不設(shè)置 相關(guān)代碼: ? 第二步 ,創(chuàng)建組件:在項(xiàng)目跟目

    2024年02月15日
    瀏覽(31)
  • 基于python天津二手房數(shù)據(jù)爬蟲采集系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)(django框架)帶效果圖

    基于python天津二手房數(shù)據(jù)爬蟲采集系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)(django框架)帶效果圖

    ?博主介紹 :黃菊華老師《Vue.js入門與商城開發(fā)實(shí)戰(zhàn)》《微信小程序商城開發(fā)》圖書作者,CSDN博客專家,在線教育專家,CSDN鉆石講師;專注大學(xué)生畢業(yè)設(shè)計(jì)教育和輔導(dǎo)。 所有項(xiàng)目都配有從入門到精通的基礎(chǔ)知識(shí)視頻課程,免費(fèi) 項(xiàng)目配有對(duì)應(yīng)開發(fā)文檔、開題報(bào)告、任務(wù)書、

    2024年02月03日
    瀏覽(23)
  • 如何使用Midjourney輔助建筑平面設(shè)計(jì)和室內(nèi)設(shè)計(jì),常用的建筑平面效果圖提示和使用效果展示(內(nèi)附Midjourney提示詞網(wǎng)站)

    如何使用Midjourney輔助建筑平面設(shè)計(jì)和室內(nèi)設(shè)計(jì),常用的建筑平面效果圖提示和使用效果展示(內(nèi)附Midjourney提示詞網(wǎng)站)

    Midjourney使用要的參數(shù)描述: --s :表示生成過程傾向于模型默認(rèn)樣式的程度(范圍為0-1000,默認(rèn)值為100) --seed — 保持生成一致 :: — 優(yōu)先級(jí)和優(yōu)先級(jí)的提示部分 --no (negative prompts) — 明確你不想要什么 --ar :設(shè)置寬高比?!?6:9(風(fēng)景)和—9:16(人像)適用于建筑攝影圖像。在攝影中

    2024年02月06日
    瀏覽(32)
  • 最強(qiáng)AI軟件教程來了!教你如何使用stable diffusion快速出景觀建筑效果圖

    最強(qiáng)AI軟件教程來了!教你如何使用stable diffusion快速出景觀建筑效果圖

    Stable Diffusion效果圖教程 要說哪款A(yù)I軟件最適合建筑設(shè)計(jì)類?那必然是midjourney和Stable Diffusion!之前我們也看到了他們生成的圖雖然很漂亮,但現(xiàn)有階段md生成圖對(duì)我們建筑景觀類把控不是很友好,而且md屬于收費(fèi)軟件,所以今天我們主要介紹Stable Diffusion(后簡(jiǎn)稱SD)的一些用法。

    2024年04月10日
    瀏覽(94)
  • 20230514-SmartChat測(cè)試效果圖

    E:20230514-SmartChat測(cè)試效果圖 您好,我是SmartChat,新生代智能機(jī)器人,通過運(yùn)用自然語言處理、機(jī)器學(xué)習(xí)和人工智能等高精尖技術(shù),可以與您進(jìn)行自然、流暢、有趣的對(duì)話,幫助您獲取所需的信息和服務(wù)。無論您想要寫商業(yè)計(jì)劃書、營銷策劃,還是寫作文、寫周報(bào)、解數(shù)學(xué)題等

    2024年02月04日
    瀏覽(82)
  • 3d建模渲染效果圖步驟

    3d建模渲染效果圖步驟

    3dmax效果圖的制作流程主要包括建模、渲染設(shè)置、燈光、材質(zhì)貼圖、攝影機(jī)和環(huán)境、渲染6大步驟。 在3dmax中想要制作出效果圖,首先需要在場(chǎng)景中制作出3D模型,這個(gè)過程就叫做“建?!?。建模有很多方式,比如通過3dmax內(nèi)置的幾何體創(chuàng)建立方體、球體等常見幾何形體,利用多

    2024年02月09日
    瀏覽(99)
  • Vray渲染效果圖材質(zhì)參數(shù)設(shè)置

    Vray渲染效果圖材質(zhì)參數(shù)設(shè)置

    渲染是創(chuàng)造出引人入勝視覺效果的關(guān)鍵步驟,在視覺藝術(shù)領(lǐng)域尤為重要。不過,渲染作為一個(gè)資源密集型的過程,每當(dāng)面對(duì)它時(shí),我們往往都會(huì)遭遇到時(shí)間消耗和資源利用的巨大挑戰(zhàn)。幸運(yùn)的是,有幾種方法能夠幫助我們優(yōu)化渲染,使之既高效又經(jīng)濟(jì)。例如,通過掌握一些渲

    2024年01月20日
    瀏覽(99)
  • AIGC 360全景圖 效果圖材質(zhì)替換

    AIGC 360全景圖 效果圖材質(zhì)替換

    首先感嘆一下AIGC的效果,如下圖所示 AUTOMATIC1111 WebUI Prompts Positive and Negative提示詞 LoRa 插件 LoRa: LatentLabs360 on CivitAI ControlNet 插件 Deep-checkpoints模型文件地址 Lora-Script 訓(xùn)練腳本 Panorama-Viewer查看全景圖插件 網(wǎng)絡(luò)問題自己搭梯子,一些安裝環(huán)境可以調(diào)整(gradio==3.16.2) 感謝 秋葉

    2024年02月09日
    瀏覽(84)
  • 利用ArcGIS Pro制作三維效果圖

    利用ArcGIS Pro制作三維效果圖

    1、新建工程 打開Arcgispro,新建工程,這里我們要用到的模板為全局場(chǎng)景。 這里添加的數(shù)據(jù)需要有一個(gè)字段內(nèi)容是數(shù)值的,這個(gè)字段也是接下來要進(jìn)行拉伸的字段。 3、高度拉伸 數(shù)據(jù)添加進(jìn)來后,如下圖所示,這時(shí)圖層處于2D圖層里。 這時(shí)我們點(diǎn)中該圖層,回到菜單欄,點(diǎn)擊

    2024年02月16日
    瀏覽(91)
  • 數(shù)據(jù)可視化:隨時(shí)間變化的效果圖

    數(shù)據(jù)可視化:隨時(shí)間變化的效果圖

    獲取北京、上海、江蘇、廣東四省的2008—2012年的GDP數(shù)據(jù) 在Jupyter Notebook上實(shí)現(xiàn)代碼如下:

    2023年04月12日
    瀏覽(95)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包