

/**
 *
 * author: water
 * data: 2019.7.8
 * */


// http://localhost:9595/#/?page=0&cId=1&gId=461&company=1&type=0

//  page:页面索引
//  company：公司类型
//  type：引入文件，center1，center2
//  cId：
//  gId：

//公共接口

let ips = '192.168.20.143';
let port = ':8899';
let wsport = ':9876';
let ip = 'http://' + ips + port;
let wsip = 'ws://' + ips + wsport;




import { Message } from 'element-ui';

// message.message('登录成功'+num,'success');
// _this.message("案例匹配成功",1);

//  success 1 成功   / warning 2 警告  /  info 3 消息   / error 4 错误

function message(title,type) {

  let word = '';

  switch (type){
    case 1 : word = 'success';break;
    case 2 : word = 'warning';break;
    case 3 : word = 'info';break;
    case 4 : word = 'error';break;
  }

  Message.closeAll();

  Message({
    showClose: true,
    message: title,
    type:word,
    duration:"1800",
  });
}


//回到顶部
function exitTop() {
  document.documentElement.scrollTop = "0px"
  document.body.scrollTop = "0px";
}

let wsArr = [];
let wsUrlArr = [];

function websocketFn(obj,callBackFn, t= false){

  if(!t){
    return;
  }
  wsUrlArr[obj.path] = wsip + '?gId='+obj.gId+'&cId='+obj.cId+'&path='+obj.path;
  wsArr[obj.path] = null;
  let heartCheck = {
    timeout: 10*1000,  //  心跳检测时长
    timeoutObj: null, // 定时变量
    serverTimeoutObj: null,

    start: function(ws){
      console.log('start');
      var self = this;
      this.timeoutObj && clearTimeout(this.timeoutObj);
      this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
      this.timeoutObj = setTimeout(function(){
        //这里发送一个心跳，后端收到后，返回一个心跳消息，
        console.log('55555');
        ws.send(obj.path);
        self.serverTimeoutObj = setTimeout(function() {
          console.log(111);
          console.log(ws);
          ws.close();
          // createWebSocket();
        }, self.timeout);

      }, this.timeout)
    },

  }


  // 重连
  function reconnect(url) {
    if (reconnect.lockReconnect) return;
    reconnect.lockReconnect = true;
    setTimeout(function () {     //没连接上会一直重连，设置延迟避免请求过多
      createWebSocket(url);
      reconnect.lockReconnect = false;
    }, 2000);
  }


  // 实例websocket
  function createWebSocket(url) {
    try {

      if ('WebSocket' in window) {
        wsArr[obj.path] = new WebSocket(url);
      } else if ('MozWebSocket' in window) {
        wsArr[obj.path] = new MozWebSocket(url);
      } else {

      }
      initEventHandle(wsArr[obj.path],obj);
    //   console.log(obj.path)
    } catch (e) {
      reconnect(url);
    }
  }

  // 初始化事件函数
  function initEventHandle(ws,newObj) {
    // console.log(3)

    ws.onclose = function () {
      console.log('链接关闭');
      ws = null;
      reconnect(wsUrlArr[obj.path]);
    };
    ws.onerror = function (err) {
      console.log('发生异常了');
      reconnect(wsUrlArr[obj.path]);
    };
    ws.onopen = function () {
      //心跳检测重置
      heartCheck.start(wsArr[obj.path]);
      console.log(newObj.path+"websocket连上了!");

    };
    ws.onmessage = function (msg) {

      //拿到任何消息都说明当前连接是正常的
      console.log('接收到消息');
      heartCheck.start(wsArr[obj.path]);

      let data = JSON.parse(msg.data);
      callBackFn(data);
    };
  }

  createWebSocket(wsUrlArr[obj.path]);


}


export default {
  ip,
  message,
  exitTop,
  websocketFn,
}
