import website from "@/config/website";
import {getStore} from "@/util/store";
import {getUserInfo} from '@/api/user'

const userInfo = getStore({name: 'userInfo'}) || [];
let ws = {};
let socket
ws.setInitSocket = function (uid) {
  const user_id = userInfo.user_id;
  socket = new WebSocket(website.socket + user_id);
  socket.onopen = function (evt) {
    console.log('link success');
  };
  socket.onclose = function (evt) {
    console.log('link break')
  };
  let that = this
  socket.onmessage = function ({data}) {
    if (data) {
      if (JSON.parse(data) !== null) {
        ws.msgCount = JSON.parse(data)
      }
    }
    //console.log(data, '消息信息')
  };
  socket.onerror = function ({data}) {
    console.log('link error:', data)
  };

}
ws.socketSend = function () {
  const data = "1,1,manager";
  //console.log('发送消息:', data)
  if (socket) {
    socket.send(data);
  } else {
    setTimeout(() => {
      socket.send(data);
    }, 1000)
  }
}
export default ws;
