如果不允许 websockets,则 Blazor 服务器托管显示警报
Blazor server hosted display alert if websockets not allowed
我想创建一个托管的 Blazor 服务器。有没有办法显示浏览器警报或将用户重定向到另一个 url,例如如果 Blazor Wasm 是 运行,如果 SignalR 由于不允许 websockets 而无法创建连接?
这可以使用 c# 或 Javascript 完成吗?
您可以通过一些 Javascript 实现以下逻辑来做到这一点:尝试使用 Web 套接字进行连接,如果失败,则重定向。
这是我自己在 TypeScript:
中的工作示例
public startConnection = () => {
this.hubConnection = new signalR.HubConnectionBuilder()
.configureLogging(signalR.LogLevel.Debug)
.withUrl('http://localhost:20000/yourHub', signalR.HttpTransportType.WebSockets)
.build();
this.hubConnection
.start()
.then(() => {
console.log('Connected!');
})
.catch(err => {
console.log('Error while starting connection: ' + err));
// do the redirect stuff here...
}
}
如果您不熟悉 Javascript,您可以开始阅读 Javascript 客户端的 Microsoft Documentation 。
我想创建一个托管的 Blazor 服务器。有没有办法显示浏览器警报或将用户重定向到另一个 url,例如如果 Blazor Wasm 是 运行,如果 SignalR 由于不允许 websockets 而无法创建连接?
这可以使用 c# 或 Javascript 完成吗?
您可以通过一些 Javascript 实现以下逻辑来做到这一点:尝试使用 Web 套接字进行连接,如果失败,则重定向。 这是我自己在 TypeScript:
中的工作示例 public startConnection = () => {
this.hubConnection = new signalR.HubConnectionBuilder()
.configureLogging(signalR.LogLevel.Debug)
.withUrl('http://localhost:20000/yourHub', signalR.HttpTransportType.WebSockets)
.build();
this.hubConnection
.start()
.then(() => {
console.log('Connected!');
})
.catch(err => {
console.log('Error while starting connection: ' + err));
// do the redirect stuff here...
}
}
如果您不熟悉 Javascript,您可以开始阅读 Javascript 客户端的 Microsoft Documentation 。