File size: 12,549 Bytes
b66240d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
 * WebSocket Client for Real-time Communication
 * Manages WebSocket connections with automatic reconnection and exponential backoff
 * Supports message routing to type-specific subscribers
 */
class WSClient {
    constructor() {
        this.socket = null;
        this.status = 'disconnected';
        this.statusSubscribers = new Set();
        this.globalSubscribers = new Set();
        this.typeSubscribers = new Map();
        this.eventLog = [];
        this.backoff = 1000; // Initial backoff delay in ms
        this.maxBackoff = 16000; // Maximum backoff delay in ms
        this.shouldReconnect = true;
        this.reconnectAttempts = 0;
        this.connectionStartTime = null;
    }

    /**
     * Automatically determine WebSocket URL based on current window location
     * Always uses the current origin to avoid hardcoded URLs
     */
    get url() {
        const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
        const host = window.location.host;
        return `${protocol}//${host}/ws`;
    }

    /**
     * Log WebSocket events for debugging and monitoring
     * Maintains a rolling buffer of the last 100 events
     * @param {Object} event - Event object to log
     */
    logEvent(event) {
        const entry = { 
            ...event, 
            time: new Date().toISOString(),
            attempt: this.reconnectAttempts 
        };
        this.eventLog.push(entry);
        // Keep only last 100 events
        if (this.eventLog.length > 100) {
            this.eventLog = this.eventLog.slice(-100);
        }
        console.log('[WSClient]', entry);
    }

    /**
     * Subscribe to connection status changes
     * @param {Function} callback - Called with new status ('connecting', 'connected', 'disconnected', 'error')
     * @returns {Function} Unsubscribe function
     */
    onStatusChange(callback) {
        if (typeof callback !== 'function') {
            throw new Error('Callback must be a function');
        }
        this.statusSubscribers.add(callback);
        // Immediately call with current status
        callback(this.status);
        return () => this.statusSubscribers.delete(callback);
    }

    /**
     * Subscribe to all WebSocket messages
     * @param {Function} callback - Called with parsed message data
     * @returns {Function} Unsubscribe function
     */
    onMessage(callback) {
        if (typeof callback !== 'function') {
            throw new Error('Callback must be a function');
        }
        this.globalSubscribers.add(callback);
        return () => this.globalSubscribers.delete(callback);
    }

    /**
     * Subscribe to specific message types
     * @param {string} type - Message type to subscribe to (e.g., 'market_update', 'news_update')
     * @param {Function} callback - Called with messages of the specified type
     * @returns {Function} Unsubscribe function
     */
    subscribe(type, callback) {
        if (typeof callback !== 'function') {
            throw new Error('Callback must be a function');
        }
        if (!this.typeSubscribers.has(type)) {
            this.typeSubscribers.set(type, new Set());
        }
        const set = this.typeSubscribers.get(type);
        set.add(callback);
        return () => set.delete(callback);
    }

    /**
     * Update connection status and notify all subscribers
     * @param {string} newStatus - New status value
     */
    updateStatus(newStatus) {
        if (this.status !== newStatus) {
            const oldStatus = this.status;
            this.status = newStatus;
            this.logEvent({ 
                type: 'status_change', 
                from: oldStatus, 
                to: newStatus 
            });
            this.statusSubscribers.forEach(cb => {
                try {
                    cb(newStatus);
                } catch (error) {
                    console.error('[WSClient] Error in status subscriber:', error);
                }
            });
        }
    }

    /**
     * Establish WebSocket connection with automatic reconnection
     * Implements exponential backoff for reconnection attempts
     */
    connect() {
        // Prevent multiple simultaneous connection attempts
        if (this.socket && (this.socket.readyState === WebSocket.CONNECTING || this.socket.readyState === WebSocket.OPEN)) {
            console.log('[WSClient] Already connected or connecting');
            return;
        }

        this.connectionStartTime = Date.now();
        this.updateStatus('connecting');
        
        try {
            this.socket = new WebSocket(this.url);
            this.logEvent({ 
                type: 'connection_attempt', 
                url: this.url,
                attempt: this.reconnectAttempts + 1
            });

            this.socket.onopen = () => {
                const connectionTime = Date.now() - this.connectionStartTime;
                this.backoff = 1000; // Reset backoff on successful connection
                this.reconnectAttempts = 0;
                this.updateStatus('connected');
                this.logEvent({ 
                    type: 'connection_established', 
                    connectionTime: `${connectionTime}ms`
                });
                console.log(`[WSClient] Connected to ${this.url} in ${connectionTime}ms`);
            };

            this.socket.onmessage = (event) => {
                try {
                    const data = JSON.parse(event.data);
                    this.logEvent({ 
                        type: 'message_received', 
                        messageType: data.type || 'unknown',
                        size: event.data.length 
                    });
                    
                    // Notify global subscribers
                    this.globalSubscribers.forEach(cb => {
                        try {
                            cb(data);
                        } catch (error) {
                            console.error('[WSClient] Error in global subscriber:', error);
                        }
                    });
                    
                    // Notify type-specific subscribers
                    if (data.type && this.typeSubscribers.has(data.type)) {
                        this.typeSubscribers.get(data.type).forEach(cb => {
                            try {
                                cb(data);
                            } catch (error) {
                                console.error(`[WSClient] Error in ${data.type} subscriber:`, error);
                            }
                        });
                    }
                } catch (error) {
                    console.error('[WSClient] Message parse error:', error);
                    this.logEvent({ 
                        type: 'parse_error', 
                        error: error.message,
                        rawData: event.data.substring(0, 100) 
                    });
                }
            };

            this.socket.onclose = (event) => {
                const wasConnected = this.status === 'connected';
                this.updateStatus('disconnected');
                this.logEvent({ 
                    type: 'connection_closed', 
                    code: event.code,
                    reason: event.reason || 'No reason provided',
                    wasClean: event.wasClean
                });
                
                // Attempt reconnection if enabled
                if (this.shouldReconnect) {
                    this.reconnectAttempts++;
                    const delay = this.backoff;
                    this.backoff = Math.min(this.backoff * 2, this.maxBackoff);
                    
                    console.log(`[WSClient] Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts})...`);
                    this.logEvent({ 
                        type: 'reconnect_scheduled', 
                        delay: `${delay}ms`,
                        nextBackoff: `${this.backoff}ms`
                    });
                    
                    setTimeout(() => this.connect(), delay);
                }
            };

            this.socket.onerror = (error) => {
                console.error('[WSClient] WebSocket error:', error);
                this.updateStatus('error');
                this.logEvent({ 
                    type: 'connection_error', 
                    error: error.message || 'Unknown error',
                    readyState: this.socket ? this.socket.readyState : 'null'
                });
            };
        } catch (error) {
            console.error('[WSClient] Failed to create WebSocket:', error);
            this.updateStatus('error');
            this.logEvent({ 
                type: 'creation_error', 
                error: error.message 
            });
            
            // Retry connection if enabled
            if (this.shouldReconnect) {
                this.reconnectAttempts++;
                const delay = this.backoff;
                this.backoff = Math.min(this.backoff * 2, this.maxBackoff);
                setTimeout(() => this.connect(), delay);
            }
        }
    }

    /**
     * Gracefully disconnect WebSocket and disable automatic reconnection
     */
    disconnect() {
        this.shouldReconnect = false;
        if (this.socket) {
            this.logEvent({ type: 'manual_disconnect' });
            this.socket.close(1000, 'Client disconnect');
            this.socket = null;
        }
    }

    /**
     * Manually trigger reconnection (useful for testing or recovery)
     */
    reconnect() {
        this.disconnect();
        this.shouldReconnect = true;
        this.backoff = 1000; // Reset backoff
        this.reconnectAttempts = 0;
        this.connect();
    }

    /**
     * Send a message through the WebSocket connection
     * @param {Object} data - Data to send (will be JSON stringified)
     * @returns {boolean} True if sent successfully, false otherwise
     */
    send(data) {
        if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
            console.error('[WSClient] Cannot send message: not connected');
            this.logEvent({ 
                type: 'send_failed', 
                reason: 'not_connected',
                readyState: this.socket ? this.socket.readyState : 'null'
            });
            return false;
        }

        try {
            const message = JSON.stringify(data);
            this.socket.send(message);
            this.logEvent({ 
                type: 'message_sent', 
                messageType: data.type || 'unknown',
                size: message.length 
            });
            return true;
        } catch (error) {
            console.error('[WSClient] Failed to send message:', error);
            this.logEvent({ 
                type: 'send_error', 
                error: error.message 
            });
            return false;
        }
    }

    /**
     * Get a copy of the event log
     * @returns {Array} Array of logged events
     */
    getEvents() {
        return [...this.eventLog];
    }

    /**
     * Get current connection statistics
     * @returns {Object} Connection statistics
     */
    getStats() {
        return {
            status: this.status,
            reconnectAttempts: this.reconnectAttempts,
            currentBackoff: this.backoff,
            maxBackoff: this.maxBackoff,
            shouldReconnect: this.shouldReconnect,
            subscriberCounts: {
                status: this.statusSubscribers.size,
                global: this.globalSubscribers.size,
                typed: Array.from(this.typeSubscribers.entries()).map(([type, subs]) => ({
                    type,
                    count: subs.size
                }))
            },
            eventLogSize: this.eventLog.length,
            url: this.url
        };
    }

    /**
     * Check if WebSocket is currently connected
     * @returns {boolean} True if connected
     */
    isConnected() {
        return this.socket && this.socket.readyState === WebSocket.OPEN;
    }

    /**
     * Clear all subscribers (useful for cleanup)
     */
    clearSubscribers() {
        this.statusSubscribers.clear();
        this.globalSubscribers.clear();
        this.typeSubscribers.clear();
        this.logEvent({ type: 'subscribers_cleared' });
    }
}

// Create singleton instance
const wsClient = new WSClient();

// Auto-connect on module load
wsClient.connect();

// Export singleton instance
export default wsClient;