Skip to content

Blazor Disable Non-WebSockets Transports by Default #34644

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 16 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public static ComponentEndpointConventionBuilder MapBlazorHub(
throw new ArgumentNullException(nameof(path));
}

return endpoints.MapBlazorHub(path, configureOptions: _ => { });
// Only support the WebSockets transport type by default
return endpoints.MapBlazorHub(path, configureOptions: options => { options.Transports = HttpTransportType.WebSockets; });
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Web.JS/dist/Release/blazor.server.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Components/Web.JS/dist/Release/blazor.webview.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/Components/Web.JS/src/Boot.Server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DotNet } from '@microsoft/dotnet-js-interop';
import { Blazor } from './GlobalExports';
import { HubConnectionBuilder, HubConnection } from '@microsoft/signalr';
import { HubConnectionBuilder, HubConnection, HttpTransportType } from '@microsoft/signalr';
import { MessagePackHubProtocol } from '@microsoft/signalr-protocol-msgpack';
import { showErrorNotification } from './BootErrors';
import { shouldAutoStart } from './BootCommon';
Expand Down Expand Up @@ -85,7 +85,7 @@ async function initializeConnection(options: CircuitStartOptions, logger: Logger
(hubProtocol as unknown as { name: string }).name = 'blazorpack';

const connectionBuilder = new HubConnectionBuilder()
.withUrl('_blazor')
.withUrl('_blazor', HttpTransportType.WebSockets)
.withHubProtocol(hubProtocol);

options.configureSignalR(connectionBuilder);
Expand Down Expand Up @@ -130,6 +130,10 @@ async function initializeConnection(options: CircuitStartOptions, logger: Logger
await connection.start();
} catch (ex) {
unhandledError(connection, ex, logger);

if (ex.message.includes('UnsupportedTransportWebSocketsError')) {
showErrorNotification('Unable to connect, please ensure you are using an updated browser and WebSockets are available.');
}
}

DotNet.attachDispatcher({
Expand Down
6 changes: 5 additions & 1 deletion src/Components/Web.JS/src/BootErrors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
let hasFailed = false;

export async function showErrorNotification() {
export async function showErrorNotification(customErrorMessage: string = '') {
let errorUi = document.querySelector('#blazor-error-ui') as HTMLElement;
if (errorUi) {
errorUi.style.display = 'block';

if (customErrorMessage && errorUi.firstChild) {
errorUi.firstChild.textContent = `\n\t${customErrorMessage}\t\n`;
}
}

if (!hasFailed) {
Expand Down
4 changes: 3 additions & 1 deletion src/SignalR/clients/ts/signalr/src/HttpConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,9 @@ export class HttpConnection implements IConnection {
if ((transport === HttpTransportType.WebSockets && !this._options.WebSocket) ||
(transport === HttpTransportType.ServerSentEvents && !this._options.EventSource)) {
this._logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it is not supported in your environment.'`);
return new Error(`'${HttpTransportType[transport]}' is not supported in your environment.`);
const unsupportedTransportError = new Error(`'${HttpTransportType[transport]}' is not supported in your environment.`);
unsupportedTransportError.name = `UnsupportedTransport${HttpTransportType[transport]}Error`;
return unsupportedTransportError;
} else {
this._logger.log(LogLevel.Debug, `Selecting transport '${HttpTransportType[transport]}'.`);
try {
Expand Down