Skip to content

Commit

Permalink
feet: connect when simbridge status changes, not automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjozork committed Mar 31, 2024
1 parent e4635e1 commit 5bf28a1
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions fbw-common/src/systems/instruments/src/remote-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Wait } from '@microsoft/msfs-sdk';
import { NXDataStore } from '@flybywiresim/fbw-sdk';
import { ClientState, NXDataStore, SimBridgeClientState } from '@flybywiresim/fbw-sdk';
import { protocolV0 } from '@flybywiresim/remote-bridge-types';
import { Base64 } from 'js-base64';

Expand Down Expand Up @@ -80,7 +80,14 @@ export class RemoteClient {
this.clientName = options.clientName;
this.fileDownloadBasePath = options.fileDownloadBasePath;

this.attemptConnect();
ClientState.getInstance().simBridgeConnectionState.sub((state) => {
if (state === SimBridgeClientState.CONNECTED) {
this.attemptConnect();
} else if (this.ws) {
this.ws.close();
}
});

this.fetchInstrumentsMetadata(options.instrumentsMetadataFile).then((metadata) => this.instruments = metadata);
}

Expand Down Expand Up @@ -109,6 +116,13 @@ export class RemoteClient {
}

private attemptConnect(): void {
const simBridgeClientState = ClientState.getInstance().getSimBridgeClientState();

if (simBridgeClientState !== SimBridgeClientState.CONNECTED) {
console.log(`[RemoteClient](attemptConnect) Not attempting to connect, as SimBridge client is in state "${simBridgeClientState}"`);
return;
}

this.connectionAttemptCount++;

console.log(`[RemoteClient](attemptConnect) Attempting to connect (${this.url}). attempt #${this.connectionAttemptCount}`);
Expand Down

0 comments on commit 5bf28a1

Please sign in to comment.