Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR - How do I disable WebSockets

Tags:

signalr

I upgraded to .NET 4.5, now SignalR seems insistent on using WebSockets in Firefox/Chrome - even though I'm only on Windows 7 which doesn't have a WebSocket Server.

The request obviously fails:

Firefox can't establish a connection to the server at ws://www.site.local/signalr?connectionData=

How do I force SignalR to forget about Websockets and use Long Polling, or is there a way of setting up Websockets for Windows 7 that I'm missing?

Update.... I'm using SignalR 0.4:

  <package id="SignalR" version="0.4.0" />
  <package id="SignalR.Hosting.AspNet" version="0.4.0.0" />
  <package id="SignalR.Js" version="0.4.0.1" />
  <package id="SignalR.Server" version="0.4.0.0" />
  <package id="SignalR.StructureMap" version="0.4.1" />
like image 328
reach4thelasers Avatar asked Apr 03 '12 13:04

reach4thelasers


People also ask

How do I disable WebSockets?

When you want to turn off WebSockets, click on the TamperMonkey icon and the toggle switch to enable blocking. Refresh the page. Disable the script when you no longer want to block WebSockets.

Does SignalR use WebSockets?

SignalR uses the new WebSocket transport where available and falls back to older transports where necessary. While you could certainly write your app using WebSocket directly, using SignalR means that a lot of the extra functionality you would need to implement is already done for you.

What can block WebSocket connection?

A blocked connection can be caused by: AdBlocker / Cookie blocker browser extensions. Antivirus and Firewall software. Proxy and VPN connections.


3 Answers

I found the answer here:

https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client

Basically:

$.connection.hubs.start({ transport: 'longPolling' }, function() {
    console.log('connection started!');
});
like image 162
reach4thelasers Avatar answered Oct 19 '22 12:10

reach4thelasers


In order to disable a transport on the server side, you must use something like this:

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Transports;
using Owin;
namespace MyApplication
{
    public static class Startup
    {
        public static void ConfigureSignalR(IAppBuilder app)
        {
            // If using the global dependency resolver
            TurnOfForeverFrame(GlobalHost.DependencyResolver);
            app.MapSignalR();
        }
        public static void TurnOfForeverFrame(IDependencyResolver resolver)
        {
            var transportManager = resolver.Resolve<ITransportManager>() as TransportManager;
            transportManager.Remove("foreverFrame");
        }
    }
}

The @reach4thelasers' solution only disable it in the client, but the client could re-enable the transport and connect.

Cheers.

like image 23
vtortola Avatar answered Oct 19 '22 11:10

vtortola


For anyone looking how to disable it on the server using asp.net core 3.1:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<ChatHub>("/chathub", options =>
        {
            options.Transports =
                HttpTransportType.WebSockets |
                HttpTransportType.LongPolling;
        });
    });
}

source: https://learn.microsoft.com/en-us/aspnet/core/signalr/configuration?view=aspnetcore-3.1&tabs=dotnet#advanced-http-configuration-options-1

like image 2
Gabriel Molter Avatar answered Oct 19 '22 11:10

Gabriel Molter