(forceClose?: boolean)
| 96 | } |
| 97 | |
| 98 | reconnect(forceClose?: boolean) { |
| 99 | if (this.noReconnect) { |
| 100 | return; |
| 101 | } |
| 102 | if (this.open) { |
| 103 | if (forceClose) { |
| 104 | this.wsConn.close(); // this will force a reconnect |
| 105 | } |
| 106 | return; |
| 107 | } |
| 108 | this.reconnectTimes++; |
| 109 | if (this.reconnectTimes > 20) { |
| 110 | dlog("cannot connect, giving up"); |
| 111 | return; |
| 112 | } |
| 113 | const timeoutArr = [0, 0, 2, 5, 10, 10, 30, 60]; |
| 114 | let timeout = 60; |
| 115 | if (this.reconnectTimes < timeoutArr.length) { |
| 116 | timeout = timeoutArr[this.reconnectTimes]; |
| 117 | } |
| 118 | if (Date.now() - this.lastReconnectTime < 500) { |
| 119 | timeout = 1; |
| 120 | } |
| 121 | if (timeout > 0) { |
| 122 | dlog(sprintf("sleeping %ds", timeout)); |
| 123 | } |
| 124 | setTimeout(() => { |
| 125 | this.connectNow(String(this.reconnectTimes)); |
| 126 | }, timeout * 1000); |
| 127 | } |
| 128 | |
| 129 | onclose(event: CloseEvent) { |
| 130 | // console.log("close", event); |
no test coverage detected