SignalR - How do I disable WebSockets

后端 未结 3 1109
日久生厌
日久生厌 2020-12-01 16:23

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.

<
相关标签:
3条回答
  • 2020-12-01 16:58

    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!');
    });
    
    0 讨论(0)
  • 2020-12-01 17:00

    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.

    0 讨论(0)
  • 2020-12-01 17:12

    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://docs.microsoft.com/en-us/aspnet/core/signalr/configuration?view=aspnetcore-3.1&tabs=dotnet#advanced-http-configuration-options-1

    0 讨论(0)
提交回复
热议问题