(ioEvent?: IoEvent)
| 460 | } |
| 461 | |
| 462 | private async send (ioEvent?: IoEvent): Promise<void> { |
| 463 | if (!this.ws) { |
| 464 | throw new Error('no ws') |
| 465 | } |
| 466 | |
| 467 | const ws = this.ws |
| 468 | |
| 469 | if (ioEvent) { |
| 470 | log.silly('Io', 'send(%s)', JSON.stringify(ioEvent)) |
| 471 | this.eventBuffer.push(ioEvent) |
| 472 | } else { log.silly('Io', 'send()') } |
| 473 | |
| 474 | if (!this.connected()) { |
| 475 | log.verbose('Io', 'send() without a connected websocket, eventBuffer.length = %d', this.eventBuffer.length) |
| 476 | return |
| 477 | } |
| 478 | |
| 479 | const list: Array<Promise<any>> = [] |
| 480 | while (this.eventBuffer.length) { |
| 481 | const data = JSON.stringify( |
| 482 | this.eventBuffer.shift(), |
| 483 | ) |
| 484 | const p = new Promise<void>((resolve, reject) => ws.send( |
| 485 | data, |
| 486 | (err: undefined | Error) => { |
| 487 | if (err) { |
| 488 | reject(err) |
| 489 | } else { |
| 490 | resolve() |
| 491 | } |
| 492 | }, |
| 493 | )) |
| 494 | list.push(p) |
| 495 | } |
| 496 | |
| 497 | try { |
| 498 | await Promise.all(list) |
| 499 | } catch (e) { |
| 500 | log.error('Io', 'send() exception: %s', e.stack) |
| 501 | throw e |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | public async stop (): Promise<void> { |
| 506 | log.verbose('Io', 'stop()') |
no test coverage detected