Skip to content

Add closed bool to prevent reconnection #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/vanilla/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/vanilla/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const connect = async () => {
credential: {
type: 'api-key',
payload: API_KEY,
authEntity: API_KEY_ID,
},
authEntity: API_KEY_ID,
signalingAddress: 'https://app.viam.com:443',
Expand Down
10 changes: 10 additions & 0 deletions src/robot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export class RobotClient extends EventDispatcher implements Robot {

private savedCreds: Credentials | undefined;

private closed: boolean;

private robotServiceClient: RobotServiceClient | undefined;

private armServiceClient: ArmServiceClient | undefined;
Expand Down Expand Up @@ -182,6 +184,8 @@ export class RobotClient extends EventDispatcher implements Robot {
this.emit('connectionstatechange', { eventType });
});
}

this.closed = false;
}

private onDisconnect(event?: Event) {
Expand All @@ -191,6 +195,10 @@ export class RobotClient extends EventDispatcher implements Robot {
return;
}

if (this.closed) {
return;
}

// eslint-disable-next-line no-console
console.debug('Connection closed, will try to reconnect');
void backOff(async () => this.connect(), {
Expand Down Expand Up @@ -395,6 +403,7 @@ export class RobotClient extends EventDispatcher implements Robot {
this.peerConn = undefined;
}
this.sessionManager.reset();
this.closed = true;
this.emit(MachineConnectionEvent.DISCONNECTED, {});
}

Expand All @@ -411,6 +420,7 @@ export class RobotClient extends EventDispatcher implements Robot {
dialTimeout,
}: ConnectOptions = {}) {
this.emit(MachineConnectionEvent.CONNECTING, {});
this.closed = false;

if (this.connecting) {
// This lint is clearly wrong due to how the event loop works such that after an await, the condition may no longer be true.
Expand Down