"use strict";
var common_vendor = require("../common/vendor.js");
class webSocketClass {
  constructor(url, time) {
    this.url = url;
    this.data = null;
    this.isCreate = false;
    this.isConnect = false;
    this.isInitiative = false;
    this.timeoutNumber = time;
    this.heartbeatTimer = null;
    this.reconnectTimer = null;
    this.socketExamples = null;
    this.againTime = 3;
  }
  initSocket() {
    const _this = this;
    return new Promise((resolve, reject) => {
      this.socketExamples = common_vendor.index.connectSocket({
        url: _this.url,
        header: {
          "content-type": "application/json"
        },
        success: (res) => {
          _this.isCreate = true;
          console.log(res);
          resolve(res);
        },
        fail: (rej) => {
          console.error(rej);
          _this.isCreate = false;
          reject(rej);
        }
      });
      this.createSocket();
    });
  }
  createSocket() {
    if (this.isCreate) {
      console.log("WebSocket \u5F00\u59CB\u521D\u59CB\u5316");
      try {
        this.socketExamples.onOpen(() => {
          console.log("WebSocket \u8FDE\u63A5\u6210\u529F");
          this.isConnect = true;
          clearInterval(this.heartbeatTimer);
          clearTimeout(this.reconnectTimer);
          this.heartbeatCheck();
        });
        this.socketExamples.onMessage((res) => {
          console.log("\u6536\u5230\u6D88\u606F", res);
          common_vendor.index.$emit("message", res);
        });
        this.socketExamples.onClose(() => {
          console.log("WebSocket \u5173\u95ED\u4E86");
          this.isConnect = false;
          this.reconnect();
        });
        this.socketExamples.onError((res) => {
          console.log("WebSocket \u51FA\u9519\u4E86");
          console.log(res);
          this.isInitiative = false;
        });
      } catch (error) {
        console.warn(error);
      }
    } else {
      console.warn("WebSocket \u521D\u59CB\u5316\u5931\u8D25!");
    }
  }
  sendMsg(value) {
    const param = JSON.stringify(value);
    return new Promise((resolve, reject) => {
      this.socketExamples.send({
        data: param,
        success() {
          console.log("\u6D88\u606F\u53D1\u9001\u6210\u529F");
          resolve(true);
        },
        fail(error) {
          console.log("\u6D88\u606F\u53D1\u9001\u5931\u8D25");
          reject(error);
        }
      });
    });
  }
  heartbeatCheck() {
    console.log("\u5F00\u542F\u5FC3\u8DF3");
    this.data = { state: 1, method: "heartbeat" };
    this.heartbeatTimer = setInterval(() => {
      this.sendMsg(this.data);
    }, this.timeoutNumber * 1e3);
  }
  reconnect() {
    clearTimeout(this.reconnectTimer);
    clearInterval(this.heartbeatTimer);
    if (!this.isInitiative) {
      this.reconnectTimer = setTimeout(() => {
        this.initSocket();
      }, this.againTime * 1e3);
    }
  }
  closeSocket(reason = "\u5173\u95ED") {
    const _this = this;
    this.socketExamples.close({
      reason,
      success() {
        _this.data = null;
        _this.isCreate = false;
        _this.isConnect = false;
        _this.isInitiative = true;
        _this.socketExamples = null;
        clearInterval(_this.heartbeatTimer);
        clearTimeout(_this.reconnectTimer);
        console.log("\u5173\u95ED WebSocket \u6210\u529F");
      },
      fail() {
        console.log("\u5173\u95ED WebSocket \u5931\u8D25");
      }
    });
  }
}
exports.webSocketClass = webSocketClass;
