
class WebSocketService {
  private static instance: WebSocketService;
  private websocket: WebSocket;
  private reconnectInterval?: NodeJS.Timeout;
  private heartbeatInterval?: NodeJS.Timeout;
  private url: string;
  private openJson: string

  private constructor(url: string, openJson: string) {
    this.url = url;
    this.openJson = openJson;
    this.websocket = new WebSocket(url);
    // 首次连接
    this.websocket.onopen = () => {
      console.log('WebSocket===>建立链接');
      sessionStorage.setItem("isOutPage", "0")
      this.clearReconnectInterval();//关闭重连
      this.stopHeartbeat();//关闭心跳
      if (openJson) {//首次握手请求数据
        if(this.websocket.readyState === 1){
          console.log("首次请求====>", this.openJson,this.websocket.readyState)
          this.websocket?.send(this.openJson)
        }
      }
      this.startHeartbeat();//开启心跳
    };
    //关闭服务
    this.websocket.onclose = () => {
      console.log('WebSocket===>服务中断', sessionStorage.getItem("isOutPage"), sessionStorage.getItem("isOutPage") == "1" ? "不需要重连" : "当前页面需要重连===>");
      this.stopHeartbeat();
      this.clearReconnectInterval();//关闭重连
      // if(sessionStorage.getItem("isOutPage")  && sessionStorage.getItem("isOutPage") == "0"){
      //   console.log("当前页面需要重连===>")
      //   this.reconnect();
      // }
    };
  }
  //获取实例
  public static getInstance(url: string, openJson: string): WebSocketService {
    if (!WebSocketService.instance ) {
      console.log("new WebSocketService")
      WebSocketService.instance = new WebSocketService(url, openJson);
    }
    if( WebSocketService.instance.websocket.readyState !== 1){
      WebSocketService.instance.connect();
    }
    return WebSocketService.instance;
  }
  //重连
  public connect(): void {
    if (this.websocket.readyState === WebSocket.OPEN) return;
    this.websocket = new WebSocket(this.url);
    this.websocket.onopen = () => {
      console.log('WebSocket===>建立链接');
      sessionStorage.setItem("isOutPage", "0")
      this.clearReconnectInterval();//关闭重连
      this.stopHeartbeat();//关闭心跳
      if (this.openJson) {//首次握手请求数据
        if(this.websocket.readyState === 1){
          console.log("首次请求====>", this.openJson,this.websocket.readyState)
          this.websocket?.send(this.openJson)
        }
      }
      this.startHeartbeat();//开启心跳
    };
    this.websocket.onclose = () => {
      console.log('WebSocket===>服务中断', sessionStorage.getItem("isOutPage"), sessionStorage.getItem("isOutPage") == "1" ? "不需要重连" : "当前页面需要重连===>");
      this.stopHeartbeat();
      this.clearReconnectInterval();//关闭重连
      // if(sessionStorage.getItem("isOutPage")  && sessionStorage.getItem("isOutPage") == "0"){
      //   this.reconnect();
      // }
    };
  }
  // 重连定时
  public reconnect(): void {
    console.log('this===>', this)
    this.clearReconnectInterval();
    this.reconnectInterval = setInterval(() => {
      console.log('WebSocket 重连====>');
      this.connect();
    }, 10000);
  }
  // 清除重连
  private clearReconnectInterval(): void {
    if (this.reconnectInterval) {
      clearInterval(this.reconnectInterval);
      this.reconnectInterval = undefined;
    }
  }
  // 开启心跳
  private startHeartbeat(): void {
    this.stopHeartbeat();
    this.heartbeatInterval = setInterval(() => {
      this.websocket?.send('heartbeat');
    }, 1000*60);
  }
  // 关闭心跳
  private stopHeartbeat(): void {
    if (this.heartbeatInterval) {
      clearInterval(this.heartbeatInterval);
      this.heartbeatInterval = undefined;
    }
  }
  // 发送消息
  public send(message: string): void {
    if (this.websocket.readyState === WebSocket.OPEN) {
      this.websocket.send(message);
    } else {
      console.log('WebSocket不在线无法发送消息');
    }
  }
  public getWebsocket(): WebSocket {
    return this.websocket;
  }
}
export default WebSocketService
