* Closes the socket and underlying transport. * * @param {Boolean} discard - optional, discard the transport * @return {Socket} for chaining
(discard?: boolean)
| 557 | * @return {Socket} for chaining |
| 558 | */ |
| 559 | public close(discard?: boolean) { |
| 560 | if ( |
| 561 | discard && |
| 562 | (this.readyState === class="st">"open" || this.readyState === class="st">"closing") |
| 563 | ) { |
| 564 | return this.closeTransport(discard); |
| 565 | } |
| 566 | |
| 567 | if (class="st">"open" !== this.readyState) return; |
| 568 | |
| 569 | this.readyState = class="st">"closing"; |
| 570 | |
| 571 | if (this.writeBuffer.length) { |
| 572 | debug( |
| 573 | class="st">"there are %d remaining packets in the buffer, waiting for the 'drain' event", |
| 574 | this.writeBuffer.length, |
| 575 | ); |
| 576 | this.once(class="st">"drain", () => { |
| 577 | debug(class="st">"all packets have been sent, closing the transport"); |
| 578 | this.closeTransport(discard); |
| 579 | }); |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | debug(class="st">"the buffer is empty, closing the transport right away"); |
| 584 | this.closeTransport(discard); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Closes the underlying transport. |
no test coverage detected