Skip to content

Commit

Permalink
improved monitorConnStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
jibon57 committed Oct 17, 2024
1 parent 550993d commit f13ed60
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/helpers/nats/ConnectNats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
NatsError,
wsconnect,
tokenAuthenticator,
DebugEvents,
Events,
} from '@nats-io/nats-core';
import { jetstream, JetStreamClient, JsMsg } from '@nats-io/jetstream';
import { isURL } from 'validator';
Expand Down Expand Up @@ -378,7 +380,6 @@ export default class ConnectNats {
if (this._nc?.isClosed()) {
this.messageQueue.isConnected(false);
this.endSession('notifications.room-disconnected-network-error');
store.dispatch(updateIsNatsServerConnected(false));

clearInterval(this.statusCheckerInterval);
this.statusCheckerInterval = undefined;
Expand All @@ -389,19 +390,26 @@ export default class ConnectNats {
};

for await (const s of this._nc.status()) {
if (s.type === 'reconnecting' && !this.isRoomReconnecting) {
this._setRoomConnectionStatusState('re-connecting');
store.dispatch(updateIsNatsServerConnected(false));

this.isRoomReconnecting = true;
startStatusChecker();
} else if (s.type === 'reconnect') {
this._setRoomConnectionStatusState('connected');
store.dispatch(updateIsNatsServerConnected(true));

clearInterval(this.statusCheckerInterval);
this.statusCheckerInterval = undefined;
this.isRoomReconnecting = false;
switch (s.type) {
case Events.Disconnect:
store.dispatch(updateIsNatsServerConnected(false));
break;
case DebugEvents.Reconnecting:
if (!this.isRoomReconnecting) {
this._setRoomConnectionStatusState('re-connecting');

this.isRoomReconnecting = true;
startStatusChecker();
}
break;
case Events.Reconnect:
this._setRoomConnectionStatusState('connected');
store.dispatch(updateIsNatsServerConnected(true));

clearInterval(this.statusCheckerInterval);
this.statusCheckerInterval = undefined;
this.isRoomReconnecting = false;
break;
}
}
}
Expand Down

0 comments on commit f13ed60

Please sign in to comment.