Skip to content

Blazor Server - Auto reconnect on mobile browsers #44700

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

Closed
1 change: 1 addition & 0 deletions src/Components/Web.JS/src/Boot.Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async function boot(userOptions?: Partial<CircuitStartOptions>): Promise<void> {
const reconnection = existingConnection || await initializeConnection(options, logger, circuit);
if (!(await circuit.reconnect(reconnection))) {
logger.log(LogLevel.Information, 'Reconnection attempt to the circuit was rejected by the server. This may indicate that the associated state is no longer available on the server.');
options.reconnectionHandler?.onConnectionRejected?.(options.reconnectionOptions);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export interface ReconnectionOptions {
maxRetries: number;
retryIntervalMilliseconds: number;
dialogId: string;
reloadOnCircuitRejected: boolean;
}

export interface ReconnectionHandler {
onConnectionDown(options: ReconnectionOptions, error?: Error): void;
onConnectionUp(): void;
onConnectionRejected?(options: ReconnectionOptions): void;
}

const defaultOptions: CircuitStartOptions = {
Expand All @@ -41,5 +43,6 @@ const defaultOptions: CircuitStartOptions = {
maxRetries: 8,
retryIntervalMilliseconds: 20000,
dialogId: 'components-reconnect-modal',
reloadOnCircuitRejected: false,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ export class DefaultReconnectionHandler implements ReconnectionHandler {
this._currentReconnectionProcess = null;
}
}
}

class ReconnectionProcess {
static readonly MaximumFirstRetryInterval = 3000;
onConnectionRejected(options: ReconnectionOptions) {
if (options.reloadOnCircuitRejected) {
location.reload();
}
}
};

class ReconnectionProcess {
readonly reconnectDisplay: ReconnectDisplay;

isDisposed = false;
Expand All @@ -65,12 +69,6 @@ class ReconnectionProcess {
async attemptPeriodicReconnection(options: ReconnectionOptions) {
for (let i = 0; i < options.maxRetries; i++) {
this.reconnectDisplay.update(i + 1);

const delayDuration = i === 0 && options.retryIntervalMilliseconds > ReconnectionProcess.MaximumFirstRetryInterval
? ReconnectionProcess.MaximumFirstRetryInterval
: options.retryIntervalMilliseconds;
await this.delay(delayDuration);

if (this.isDisposed) {
break;
}
Expand All @@ -91,6 +89,7 @@ class ReconnectionProcess {
// We got an exception so will try again momentarily
this.logger.log(LogLevel.Error, err as Error);
}
await this.delay(options.retryIntervalMilliseconds);
}

this.reconnectDisplay.failed();
Expand Down