zoukankan      html  css  js  c++  java
  • vue中WebSocket

    created(){
        this.initWebSocket();
    },
    beforeDestroy() {    ////关闭时断开socket连接
        this.websocketclose();
    },
    methods:{
        initWebSocket(address) {
          let userId = this.info.id;
          let roomId = this.$route.query.id;
          let nickname = this.info.nickname;
          // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
          // 测试  115.28.185.152:8082
          // 正式  47.104.29.50:8081
          this.websock = new WebSocket(url); //这里是websocket服务地址,这里的地址可以前端根据后台地址参数规则拼成,也可以向后端请求
          this.websock.onopen = this.websocketonopen;
          this.websock.onerror = this.websocketonerror;
          this.websock.onmessage = this.websocketonmessage;
          this.websock.onclose = this.websocketclose;
           //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。Ï
          window.onbeforeunload = this.websocketclose;
        },
        websocketonopen() {
          this.playerOptions.autoplay=true;
          console.log("WebSocket连接成功");
        },
        websocketonerror(e) {
          console.log("WebSocket连接发生错误");
        },
        websocketonmessage(e) {
          let data = JSON.parse(e.data);
          if (data.type !== "1") {
            this.msgs.push(data);
          }
        },
        websocketclose(e) {
           this.websock.close()
          console.log("connection closed "); 
        },}
    
  • 相关阅读:
    LoadRunner调用java函数测试oracle
    pam_cracklib.so模块
    crontab定时任务安装、使用方法
    yum的repo文件详解、以及epel简介、yum源的更换
    ubuntu添加开机自启和sysv-rc-conf
    MySQL配置参数详解
    集群管理软件clustershell
    Mysql命令大全
    Nginx配置文件nginx.conf 详解
    linux下iptables配置详解
  • 原文地址:https://www.cnblogs.com/ljh--/p/12498514.html
Copyright © 2011-2022 走看看