import { encrypt } from "./encryption" export class AdvancedWebSocket extends WebSocket { constructor(url: string, key?: string, iv?: string) { super(url) this.key = key || '' this.iv = iv || '' } messageQueue: any[] = [] key: string = '' iv: string = '' sendData(data: any) { this.messageQueue.push(data) this.processQueue() } sendEncrypted(data: any) { if (!this.key || !this.iv) { this.sendData(data) } else { this.sendData(`~e~${encrypt(data, this.key, this.iv)}`) } } processQueue() { if (this.readyState === WebSocket.OPEN) { while (this.messageQueue.length > 0) { const data = this.messageQueue.shift() if (typeof data === 'string') { this.send(data) } else { this.send(JSON.stringify(data)) } } } else { setTimeout(() => this.processQueue(), 300) } } } export default AdvancedWebSocket