* Sends a ping and manages the timer to wait for a pong.
()
| 160 | * Sends a ping and manages the timer to wait for a pong. |
| 161 | */ |
| 162 | private sendPing(): void { |
| 163 | try { |
| 164 | if (this.client === undefined) { |
| 165 | if (!this.isMonitoring) { |
| 166 | // if monitoring stopped before the ping timer fires, its safe to return |
| 167 | this.logger.debug('stopped monitoring before ping timer fired'); |
| 168 | return; |
| 169 | } |
| 170 | const error = new Error('no client found'); |
| 171 | (error as CodedError).code = ErrorCode.KeepAliveInconsistentState; |
| 172 | throw error; |
| 173 | } |
| 174 | this.logger.debug('ping timer expired, sending ping'); |
| 175 | this.client.send('ping') |
| 176 | .then((messageId) => { |
| 177 | if (this.client === undefined) { |
| 178 | if (!this.isMonitoring) { |
| 179 | // if monitoring stopped before the ping is sent, its safe to return |
| 180 | this.logger.debug('stopped monitoring before outgoing ping message was finished'); |
| 181 | return; |
| 182 | } |
| 183 | const error = new Error('no client found'); |
| 184 | (error as CodedError).code = ErrorCode.KeepAliveInconsistentState; |
| 185 | throw error; |
| 186 | } |
| 187 | |
| 188 | this.lastPing = messageId; |
| 189 | |
| 190 | this.logger.debug('setting pong timer'); |
| 191 | |
| 192 | this.pongTimer = setTimeout( |
| 193 | () => { |
| 194 | if (this.client === undefined) { |
| 195 | // if monitoring stopped before the pong timer fires, its safe to return |
| 196 | if (!this.isMonitoring) { |
| 197 | this.logger.debug('stopped monitoring before pong timer fired'); |
| 198 | return; |
| 199 | } |
| 200 | const error = new Error('no client found'); |
| 201 | (error as CodedError).code = ErrorCode.KeepAliveInconsistentState; |
| 202 | throw error; |
| 203 | } |
| 204 | // signal that this pong is done being handled |
| 205 | this.client.off('slack_event', this.attemptAcknowledgePong); |
| 206 | |
| 207 | // no pong received to acknowledge the last ping within the serverPongTimeout |
| 208 | this.logger.debug('pong timer expired, recommend reconnect'); |
| 209 | this.recommendReconnect = true; |
| 210 | this.emit('recommend_reconnect'); |
| 211 | }, |
| 212 | this.serverPongTimeout, |
| 213 | ); |
| 214 | |
| 215 | this.client.on('slack_event', this.attemptAcknowledgePong, this); |
| 216 | }) |
| 217 | .catch((error) => { |
| 218 | this.logger.error(`Unhandled error: ${error.message}. Please report to @slack/rtm-api package maintainers.`); |
| 219 | }); |