* Closes the connection.
()
| 839 | * Closes the connection. |
| 840 | */ |
| 841 | public close() { |
| 842 | const close = () => { |
| 843 | this._onClose("forced close"); |
| 844 | debug("socket closing - telling transport to close"); |
| 845 | this.transport.close(); |
| 846 | }; |
| 847 | |
| 848 | const cleanupAndClose = () => { |
| 849 | this.off("upgrade", cleanupAndClose); |
| 850 | this.off("upgradeError", cleanupAndClose); |
| 851 | close(); |
| 852 | }; |
| 853 | |
| 854 | const waitForUpgrade = () => { |
| 855 | // wait for upgrade to finish since we can't send packets while pausing a transport |
| 856 | this.once("upgrade", cleanupAndClose); |
| 857 | this.once("upgradeError", cleanupAndClose); |
| 858 | }; |
| 859 | |
| 860 | if ("opening" === this.readyState || "open" === this.readyState) { |
| 861 | this.readyState = "closing"; |
| 862 | |
| 863 | if (this.writeBuffer.length) { |
| 864 | this.once("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