SignalR - How do I disable WebSockets

2019-01-09 12:13发布

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" />

标签: signalr
2条回答
男人必须洒脱
2楼-- · 2019-01-09 12:53

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!');
});
查看更多
戒情不戒烟
3楼-- · 2019-01-09 13:02

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.

查看更多
登录 后发表回答