Skip to content

Improve Blazor reconnection experience. #13015

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
Aug 14, 2019
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 src/Components/Web.JS/dist/Release/blazor.server.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/Components/Web.JS/src/Boot.Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ async function boot(userOptions?: Partial<BlazorOptions>): Promise<void> {
}

const reconnection = existingConnection || await initializeConnection(options, logger);
if (reconnection.state !== signalR.HubConnectionState.Connected) {
logger.log(LogLevel.Information, 'Reconnection attempt failed. Unable to connect to the server.');
return false;
}

if (!(await circuit.reconnect(reconnection))) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SignalR throws if you attempt to reconnect like this while the connection is not connected. This prevented the reconnect display from properly handling scenarios where the server went away.

Copy link
Author

@NTaylorMullen NTaylorMullen Aug 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, can't remove this entirely. It breaks the expectation of calling code, i.e. in DefaultReconnectionHandler it expects:

        // reconnectCallback will asynchronously return:
        // - true to mean success
        // - false to mean we reached the server, but it rejected the connection (e.g., unknown circuit ID)
        // - exception to mean we didn't reach the server (this can be sync or async)

In a way it makes sense. Basically, we need to have 3 different return values and instead of having an enum the previous design utilized exceptions. I'll revert this change in my next change for this experience but enable the reconnect display to handle exceptions so the UI doesn't die.

logger.log(LogLevel.Information, 'Reconnection attempt failed.');
logger.log(LogLevel.Information, 'Reconnection attempt to the circuit failed.');
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ export class DefaultReconnectDisplay implements ReconnectDisplay {
];

this.modal.style.cssText = modalStyles.join(';');
this.modal.innerHTML = '<h5 style="margin-top: 20px"></h5><button style="margin:5px auto 5px">Retry?</button><p>Alternatively, <a href>reload</a></p>';
this.modal.innerHTML = '<h5 style="margin-top: 20px"></h5><button style="margin:5px auto 5px">Retry</button><p>Alternatively, <a href>reload</a></p>';
this.message = this.modal.querySelector('h5')!;
this.button = this.modal.querySelector('button')!;
this.reloadParagraph = this.modal.querySelector('p')!;

this.button.addEventListener('click', () => window['Blazor'].reconnect());
this.button.addEventListener('click', async () => {
this.show();
const successful = await window['Blazor'].reconnect();
if (!successful) {
this.failed();
}
});
this.reloadParagraph.querySelector('a')!.addEventListener('click', () => location.reload());
}

Expand All @@ -57,7 +63,7 @@ export class DefaultReconnectDisplay implements ReconnectDisplay {

failed(): void {
this.button.style.display = 'block';
this.reloadParagraph.style.display = 'block';
this.message.textContent = 'Failed to reconnect to the server.';
this.reloadParagraph.style.display = 'none';
this.message.innerHTML = 'Reconnection failed. Try <a href>reloading</a> the page if you\'re unable to reconnect.';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('DefaultReconnectDisplay', () => {
display.failed();

expect(display.modal.style.display).toBe('block');
expect(display.message.textContent).toBe('Failed to reconnect to the server.');
expect(display.message.innerHTML).toBe('Reconnection failed. Try <a href=\"\">reloading</a> the page if you\'re unable to reconnect.');
expect(display.button.style.display).toBe('block');
});

Expand Down