Do X when SignalR fails to use websockets

Viewed 87

I want to give a warning to an User when SignalR can't establish a Websocket Connection. How can i find out if the Websocket Connection was blocked / if SignalR is using a different transport Method?

I'm using Asp.net Core SignalR and Typescript.

1 Answers

How can i find out if the Websocket Connection was blocked / if SignalR is using a different transport Method?

On SignalR JavaScript client, we can get the transport using this code snippet.

HubConnection.connection.transport.constructor.name

Test code

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub")
    .configureLogging(signalR.LogLevel.Trace)
    .build();

//...

connection.start().then(function () {
    //...
    //get the transport

    console.log(connection.connection.transport.constructor.name);
}).catch(function (err) {
    return console.error(err.toString());
});

Test Result

With WebSocket

enter image description here

With LongPolling

enter image description here

Related