fix: use heartbeat to keep ws connection alive (#12079)

This commit is contained in:
Justin Starry 2020-09-07 10:24:16 +08:00 committed by GitHub
parent 27752c4e4d
commit d4cbd0d171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 4 deletions

View File

@ -1365,7 +1365,7 @@ export type ConfirmedSignatureInfo = {
export class Connection { export class Connection {
_rpcRequest: RpcRequest; _rpcRequest: RpcRequest;
_rpcWebSocket: RpcWebSocketClient; _rpcWebSocket: RpcWebSocketClient;
_rpcWebSocketConnected: boolean = false; _rpcWebSocketHeartbeat: IntervalID | null = null;
_commitment: ?Commitment; _commitment: ?Commitment;
_blockhashInfo: { _blockhashInfo: {
@ -2669,7 +2669,10 @@ export class Connection {
* @private * @private
*/ */
_wsOnOpen() { _wsOnOpen() {
this._rpcWebSocketConnected = true; this._rpcWebSocketHeartbeat = setInterval(() => {
// Ping server every 5s to prevent idle timeouts
this._rpcWebSocket.notify('ping').catch(() => {});
}, 5000);
this._updateSubscriptions(); this._updateSubscriptions();
} }
@ -2684,7 +2687,8 @@ export class Connection {
* @private * @private
*/ */
_wsOnClose() { _wsOnClose() {
this._rpcWebSocketConnected = false; clearInterval(this._rpcWebSocketHeartbeat);
this._rpcWebSocketHeartbeat = null;
this._resetSubscriptions(); this._resetSubscriptions();
} }
@ -2777,7 +2781,7 @@ export class Connection {
return; return;
} }
if (!this._rpcWebSocketConnected) { if (this._rpcWebSocketHeartbeat === null) {
this._resetSubscriptions(); this._resetSubscriptions();
this._rpcWebSocket.connect(); this._rpcWebSocket.connect();
return; return;