在vue项目中封装并使用WebSocket(2)

创建一个websocket组件

<script>
export default {
  name: "index",
  props: {
    wsUrl: {
      type: String,
      require: true,
    },
  },
  data() {
    return {
      socket: "",
    };
  },
  watch: {
    wsUrl: {
      immediate: true,
      handler() {
        this.init();
      },
    },
  },
  methods: {
    init() {
      console.log(this.wsUrl, this.socket);
      console.log("socket-begin");
      if (this.wsUrl && this.wsUrl.trim() !== "") {
        if (typeof WebSocket === "undefined") {
          console.error("您的浏览器不支持socket");
          this.$message({
            message: "您的浏览器不支持socket",
            type: "warning",
          });
        } else {
          if (this.socket) {
            this.socket.close();
          }
          // 实例化socket
          this.socket = new WebSocket(this.wsUrl);
          // 监听socket连接
          this.socket.onopen = this.open;
          // 监听socket错误信息
          this.socket.onerror = this.error;
          // 监听socket消息
          this.socket.onmessage = this.getMessage;
          // 销毁监听
          this.socket.onclose = this.close;
        }
      }
    },
    open() {
      console.log("socket连接成功");
      this.$emit("open");
    },
    error() {
      console.log("连接错误");
      this.$emit("error");
    },
    getMessage(msg) {
      console.log(msg, msg.data);
      this.$emit("getMessage", msg);
    },
    send(msg) {
      if (this.socket) {
        this.socket.send(msg);
      } else {
        console.error("socket is not exists");
      }
    },
    close() {
      if (this.socket) {
        this.socket.close();
        console.log("socket已经关闭");
        this.$emit("close");
      } else {
        console.error("socket is not exists");
      }
    },
  },
  beforeDestroy() {
    this.close();
  },
};
</script>

在需要的组件中引用注册使用


 <template>
    <div class="view">  
      <!-- websocket连接 -->
      <WebSocket :wsUrl="wsUrl" ref="WS" @open="open" @error="error" @getMessage="getMessage" @close="close" />
    </div>
  </template>
    
  <script>
  import WebSocket from "@/components/websocket";
  export default {
    components: {
      WebSocket,
    },
    data() {
      return {
        wsUrl: "", // webSocket 链接路径
      };
    },
    methods: {
      reCallCopy(params) {
            this.wsUrl = `ws://192.168.43.248:43504/backNotifySocket`; 
      },
      // 获取信息事件
      getMessage(msg) {
        // 判断返回的数据是否是 json
        if (msg.data === "WEBSOCKET_SESSION_CLOSE") {
          this.$message({
            message: `${this.tipsText}失败!`,
            showClose: true,
            type: "warning",
          });
        } else {
          let resObj = JSON.parse(msg.data);
        }
        this.$refs.WS.close(); // 关闭链接
      },
      // 连接成功事件
      open() {
        console.log("socket连接成功!");
      },
      // 连接失败事件
      error() {
        console.log("-component-连接错误!");
        this.$message({
          message: `${this.tipsText}失败!`,
          type: "warning",
          showClose: true,
        });
        this.$refs.WS.close(); // 关闭链接
        this.wsUrl = "";
        this.$store.commit("publicModule/setViewLoading", false);
      },
      // 关闭 websocket
      close() {
        console.log("socket已经关闭,召测结束");
        this.$store.commit("publicModule/setViewLoading", false);
      },
      // #endregion
    },
  };
  </script>

相关推荐

  1. vue项目封装使用WebSocket(2)

    2024-03-24 10:42:09       23 阅读
  2. vue和uniapp使用 websocket封装js

    2024-03-24 10:42:09       7 阅读
  3. Vue项目WebSocket封装

    2024-03-24 10:42:09       41 阅读
  4. vue项目webSocket封装(传token)

    2024-03-24 10:42:09       39 阅读
  5. Vue使用websocket的流程

    2024-03-24 10:42:09       8 阅读
  6. vue3:websocket封装使用

    2024-03-24 10:42:09       35 阅读
  7. Vue websocket封装使用

    2024-03-24 10:42:09       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 10:42:09       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 10:42:09       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 10:42:09       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 10:42:09       20 阅读

热门阅读

  1. 【VSTO开发】遍历 Ribbon 中的所有控件或按钮

    2024-03-24 10:42:09       22 阅读
  2. C语言UDP基础CS模型

    2024-03-24 10:42:09       22 阅读
  3. 关于阿里云的高级运维面试题

    2024-03-24 10:42:09       23 阅读
  4. 蓝桥杯刷题--python-30

    2024-03-24 10:42:09       24 阅读
  5. C#与三菱PLC网络模块通讯

    2024-03-24 10:42:09       22 阅读
  6. go实现tcp客户端

    2024-03-24 10:42:09       21 阅读
  7. [ESP32] 编码旋钮驱动

    2024-03-24 10:42:09       18 阅读
  8. 深度学习 (自动求导)

    2024-03-24 10:42:09       17 阅读
  9. 大模型产品整理

    2024-03-24 10:42:09       17 阅读