'How can i inherit WebSockets methods and properties in JavaScript?

I am trying to modify WebSockets in JavaScript to automatically attempt reconnect by adding few features to it. For instance i use MyWebSocket.sockeRef.onmessage =(e)=>{e.data} instead of MyWebSocket.onmessage = (e)=>{e.data}, I want to make a new WebSockets class more dynamic than the build in JavaScript.

class MyWebSocket {
  constructor() {
    this.socketRef = false;
  }
  connect(url) {
    console.log("this is the url", url);
    this.socketRef = new WebSocket(url);
    this.socketRef.onopen = () => {
      console.log("websocket open");
    };
    this.socketRef.onerror = (e) => {
      console.log(e.message);
    };
    this.socketRef.onclose = (e) => {
      console.log(e.code);
      console.log("websocket is closed");
      console.log(e.wasClean);
      if (e.code !== 1000 && !e.wasClean) {
        switch (e.code) {
          case 1000:
            console.log("waiting :d");
            setTimeout(this.connect(url), 10000);
            break;
          default:
            setTimeout(this.connect(url), 5000);
        }
      }
    };
  }
  send(data) {
    this.socketRef.send(data);
  }

  disconnect() {
    this.socketRef.close();
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source