Hard
Implementing WebSocket Auto-Reconnection with Exponential Backoff in JavaScript
Complete the WebSocket manager to implement automatic reconnection with backoff:
1class WSManager {2 constructor(url) {3 this.url = url;4 this.reconnectAttempts = 0;5 this.maxReconnectAttempts = 5;6 this.messageQueue = [];7 this.handlers = new Map();8 }910 connect() {11 this.ws = new WebSocket(this.url);1213 this.ws.onopen = () => {14 ______________________________15 ______________________________16 };1718 this.ws.onclose = () => {19 ______________________________20 ______________________________21 ______________________________22 };23 }2425 send(message) {26 if (this.ws?.readyState === WebSocket.OPEN) {27 this.ws.send(JSON.stringify(message));28 } else {29 this.messageQueue.push(message);30 }31 }32}