Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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();
  }
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!