* Closes the connection.
()
| 839 | * Closes the connection. |
| 840 | */ |
| 841 | public close() { |
| 842 | const close = () => { |
| 843 | this._onClose(class="st">"forced close"); |
| 844 | debug(class="st">"socket closing - telling transport to close"); |
| 845 | this.transport.close(); |
| 846 | }; |
| 847 | |
| 848 | const cleanupAndClose = () => { |
| 849 | this.off(class="st">"upgrade", cleanupAndClose); |
| 850 | this.off(class="st">"upgradeError", cleanupAndClose); |
| 851 | close(); |
| 852 | }; |
| 853 | |
| 854 | const waitForUpgrade = () => { |
| 855 | class="cm">// wait for upgrade to finish since we can't send packets while pausing a transport |
| 856 | this.once(class="st">"upgrade", cleanupAndClose); |
| 857 | this.once(class="st">"upgradeError", cleanupAndClose); |
| 858 | }; |
| 859 | |
| 860 | if (class="st">"opening" === this.readyState || class="st">"open" === this.readyState) { |
| 861 | this.readyState = class="st">"closing"; |
| 862 | |
| 863 | if (this.writeBuffer.length) { |
| 864 | this.once(class="st">"drain", () => { |
| 865 | if (this.upgrading) { |
| 866 | waitForUpgrade(); |
| 867 | } else { |
| 868 | close(); |
| 869 | } |
| 870 | }); |
| 871 | } else if (this.upgrading) { |
| 872 | waitForUpgrade(); |
| 873 | } else { |
| 874 | close(); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | return this; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * Called upon transport error |
no test coverage detected