Open the socket; resolves once `open` fires.
()
| 69 | |
| 70 | /** Open the socket; resolves once `open` fires. */ |
| 71 | async open(): Promise<void> { |
| 72 | if (this.ws) return; |
| 73 | await new Promise<void>((resolve, reject) => { |
| 74 | const ws = new this.opts.wsImpl(this.opts.url); |
| 75 | this.ws = ws; |
| 76 | ws.once('open', () => { |
| 77 | recordReportEvent( |
| 78 | { kind: 'ws', direction: 'lifecycle', url: this.opts.url, message: 'open' }, |
| 79 | { reportDir: this.opts.reportDir }, |
| 80 | ); |
| 81 | resolve(); |
| 82 | }); |
| 83 | ws.once('error', (err) => { |
| 84 | if (this._closed) return; |
| 85 | recordReportEvent( |
| 86 | { |
| 87 | kind: 'ws', |
| 88 | direction: 'lifecycle', |
| 89 | url: this.opts.url, |
| 90 | message: 'error', |
| 91 | error: errorForReport(err), |
| 92 | }, |
| 93 | { reportDir: this.opts.reportDir }, |
| 94 | ); |
| 95 | reject(err as Error); |
| 96 | }); |
| 97 | ws.on('message', (data) => this._onMessage(data)); |
| 98 | ws.on('close', (code, reason) => this._onClose(code, String(reason ?? ''))); |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | /** JSON-stringifies and sends a frame. */ |
| 103 | send(frame: object): void { |