0
votes

I would like to create a Blazor server hosted. Is there a way to display browser alert or redirect the user to another url, for example were Blazor Wasm is running, if SignalR cannot create a connection due to websockets not being allowed?

Can this be done using c# or Javascript?

1
You can do this with Javascript with logic like: Start the connection only allowing WebSocktes, if connectio start fails, then redirect. - Kiril1512
Thanks, but I do not know any Javascript, yout a suggestion where I can start from? - Mihaimyh
First of all start reading the JavaScript SignalR documentation by Microsoft. After that you will realize how to control the connection events and act when something occurs. docs.microsoft.com/en-us/aspnet/core/signalr/… - Kiril1512
@Kiril1512 Thank, if you can, please add an answer I can mark it as accepted. - Mihaimyh
Yes, I will answer this and with a working code example :) - Kiril1512

1 Answers

0
votes

You can do this with some Javascript implementing the following logic: Try to connect using Web Sockets, if fails, redirect. Here is a my own working example in 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...
  }
}

If you not familiar with Javascript you can start read the Microsoft Documentation for Javascript client.