* Send a message through the WebSocket connection. * If not connected, queues the message and triggers reconnection. * @param message The message to send
(message: unknown)
| 113 | * @param message The message to send |
| 114 | */ |
| 115 | public send(message: unknown): void { |
| 116 | const data = JSON.stringify(message); |
| 117 | |
| 118 | // Track activity for non-heartbeat messages |
| 119 | const msgObj = message as { type?: string }; |
| 120 | if (msgObj.type !== 'heartbeat') { |
| 121 | this.lastActivityTime = Date.now(); |
| 122 | } |
| 123 | |
| 124 | if (this.ws && this.ws.readyState === WebSocket.OPEN) { |
| 125 | this.ws.send(data); |
| 126 | } else { |
| 127 | // Queue message for when connection is established |
| 128 | this.messageQueue.push(data); |
| 129 | |
| 130 | // Trigger reconnect if we have a server URL but aren't connected/connecting |
| 131 | if (this.serverUrl && !this.isConnecting) { |
| 132 | this.isConnecting = true; |
| 133 | // Reset retry count since this is user-initiated activity |
| 134 | this.retryCount = 0; |
| 135 | this.createConnection(); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Check if the WebSocket is currently connected. |
no test coverage detected