智慧保安互联网APP
zengh
2022-06-21 4efeb31d5b4921d7e851c256009486ad86bc70e2
store/mutations.js
@@ -46,43 +46,100 @@
         state.puserID = data.userID;
         state.avatar = data.avatar;
      }
      console.log('登入数据传输成功  开始进入连接')
      var url = 'ws://61.131.136.25:2086/websocket';
      uni.connectSocket({
         url: 'ws://192.168.0.111:9034/websocket'
         url: url
         // url: 'ws://192.168.0.111:2086/websocket'
      });
      var socketOpen = false;
      var heartbeatInterval = null;
      var reconnect = null;
      uni.onSocketOpen(function(res) {
         socketOpen = true;
         console.log('持续连接成功')
         if (reconnect && reconnect != null) {
            clearInterval(reconnect)
         }
         sendSocketMessage();
         // 5秒发送一次心跳
         heartbeatInterval = setInterval(() => {
            console.log('发送心跳中')
            uni.sendSocketMessage({
               data: 'ping'
            });
         }, 5000)
      });
      // 监听连接失败
      uni.onSocketError(function(res) {
         console.log('WebSocket连接打开失败,请检查!');
         //停止发送心跳
         if (heartbeatInterval && heartbeatInterval != null) {
            clearInterval(heartbeatInterval)
         }
         //如果不是人为关闭的话,进行重连
         // if (!_this.isSocketClose) {}
         reconnect = setInterval(() => { // 断线重连
            console.log(`断线重连中...123`)
            // 连接
            uni.connectSocket({
               url: url,
            });
         }, 3000)
      });
      function sendSocketMessage() {
         var data = {
                  // type: state.puserName,
                  id: state.puserID,
                  type: 'login'
               }
            // type: state.puserName,
            id: state.puserID,
            type: 'login'
         }
         if (socketOpen) {
            uni.sendSocketMessage({
               data: JSON.stringify(data)
            });
         }
      }
      uni.onSocketMessage(function(res) {
         console.log(res.data);
         var data  = JSON.parse(res.data)
         // if (data.type == 'video') {
         var data = JSON.parse(res.data)
         if (data.type == 'video') {
            // this.receiveVideo('Receiver', );
            console.log('进入接受 跳转',data.type)
            console.log('进入接受 跳转', data.type)
            uni.navigateTo({
               url: '../videoCall/videoCall?state=Receiver&type=' + data.type +'&room=' + data.roomId
               url: '../videoCall/videoCall?state=Receiver&type=' + data.type + '&room=' + data
                  .roomId + '&name=' + data.name + '&faqiid=' + data.faqiid
            });
         // }
         }
         // console.log('收到服务器内容:' + res.data);
      });
      //储存用户
      WxStorage.set("init", "true");
      WxStorage.set("name", data.userName);
@@ -90,6 +147,20 @@
      WxStorage.set("accounts", data.accounts);
      WxStorage.set("pass", data.pass);
   },
   // useWebSocket(state){
   //    uni.onSocketMessage(function(res) {
   //       console.log(res.data);
   //       var data  = JSON.parse(res.data)
   //       if (data.type == 'video') {
   //          // this.receiveVideo('Receiver', );
   //          console.log('进入接受 跳转',data.type)
   //          uni.navigateTo({
   //             url: '../videoCall/videoCall?state=Receiver&type=' + data.type +'&room=' + data.roomId + '&name=' + data.name + '&faqiid=' + data.faqiid
   //          });
   //       }
   //       // console.log('收到服务器内容:' + res.data);
   //    });
   // }
   loginReset(state) { //重置
      state.loging = false;
      state.message.useName = '过客 ';