export default class WebSocketClient {
  constructor(url) {
    this.url = process.env.VUE_APP_BASE_S + url;
    this.socket = null;
  }

  // 连接WebSocket服务器
  connect(onopen, onmessage, onclose) {
    this.socket = new WebSocket(this.url);

    this.socket.onopen = onopen

    this.socket.onmessage = onmessage

    this.socket.onclose = () => {
      console.log("WebSocket连接已关闭");
    };
  }

  // 发送消息
  send(message) {
    if (this.socket.readyState === WebSocket.OPEN) {
      this.socket.send(message);
    } else {
      console.log("WebSocket连接尚未打开");
    }
  }

  // 关闭连接
  close() {
    this.socket.close();
  }
}

// 使用WebSocketClient类
// const url = "ws://localhost:8080";
// const webSocket = new WebSocketClient(url);
// webSocket.connect();
// 发送消息
// webSocket.send("Hello, WebSocket!");

// webSocket.close();