import Client from "./ws";

export default function useWebsocket(store, path) {
  // connect0(store);
  connectPath(store, path);
}

const url = "ws://192.168.1.101:8082/QbMonitoring/websocket";
function connectPath(store, path) {
  new Client(
    {
      url: url + path,
      name: path,
    },
    (message) => {
      try {
        const data = JSON.parse(message.data);
        console.log("message", JSON.parse(message.data));

        if ("specificationChanges" in data) {
          console.log("[*] setting data1");
          // 分屏推送1数据
          store.updateData("1", data);
        } else if ("deliveryNotification" in data) {
          // 分屏推送3数据
          console.log("[*] setting data3");
          store.updateData("3", data);
        } else {
          // 分屏推送2数据
          console.log("[*] setting data2");
          store.updateData("2", data);
        }
      } catch (err) {
        console.log("[x] 未解析的ws数据: ", err);
      }
    }
  );
}

function connect0(store) {
  new Client(
    {
      url: url + "/0",
      name: "/0",
    },
    (message) => {
      try {
        const data = JSON.parse(message.data);
        console.log("message --- 0 ---> ", JSON.parse(message.data));
      } catch (err) {
        console.log("[x] 未解析的ws数据: ", err);
      }
    }
  );
}
