import SockJS from 'sockjs-client'; // 引入 SockJS依赖， 安装：npm install sockjs-client
import Stomp from 'stompjs'; // 安装：npm install stompjs
import url from '../axios/index'

let stompClient;
let dataZ;
const webSocket = {
  serverZAX(st) {
    let that = this;
    // 建立连接对象
    // let socket = new SockJS('http://192.168.1.244:8082/websocket');
    let socket = new SockJS(url + '/websocket');
    console.log(socket)
    // 获取STOMP子协议的客户端对象
    stompClient = Stomp.over(socket);
    // 向服务器发起websocket连接
    stompClient.connect({}, (frame) => {
      console.log(frame) // 连接成功的回复
      stompClient.subscribe('/user/topic/note', (greeting) => {
        console.log(greeting);
        dataZ = JSON.parse(greeting.body); // 将传过来的数据转为json格式
        if (dataZ.length !== 0) {
					st.socks.commit('changeRealData', { // 将webscoket中更新的数据放入单独的池子
            'realData': dataZ
          });
        }
      })
    }, (err) => {
      // 连接发生错误
      console.log(err)
    })
  }
}
export default webSocket;
