SignalR不使用Websocket传输与ASPNET 4.6

时间:2017-04-21 23:36:39

标签: azure websocket signalr asp.net-4.6

我们正在尝试使用独立的SignalR服务器,并且已经构建了一些我们正在使用的集线器。它们工作正常,但我无法让SignalR使用WebSockets传输。

总的来说,我很难找到有关使用WebSockets协商SignalR的要求的最新信息。有人可以帮我们弄清楚为什么没有使用WebSockets吗?

关于我们的配置:

  • SignalR服务器:
    • " Microsoft.AspNet.SignalR":" 2.2.1"
    • 框架:net462
  • 托管:
    • Azure应用服务
      • 网络套接字:开启
      • CORS:允许的起源:*
      • 定价等级:基本:1小
      • .NET Framework版本v4.6
      • SSL证书

我们使用最新版本的Chrome和Edge作为JS客户端。

以下是我们的一些SignalR服务器配置:

Startup.cs

app.UseOwinAppBuilder(map =>
{
    map.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    HubConfiguration hubConfiguration = new HubConfiguration
    {
        EnableDetailedErrors = hubConfig.EnableDetailedErrors.Value,
    };
    map.MapSignalR("/signalr", hubConfiguration);
});


public static IApplicationBuilder UseOwinAppBuilder(this IApplicationBuilder app, Action<IAppBuilder> configuration)
{
    if (app == null)
    {
        throw new ArgumentNullException(nameof(app));
    }

    if (configuration == null)
    {
        throw new ArgumentNullException(nameof(configuration));
    }

    return app.UseOwin(setup => setup(next =>
    {
        AppBuilder builder = new AppBuilder();
        IApplicationLifetime lifetime = (IApplicationLifetime)app.ApplicationServices.GetService(typeof(IApplicationLifetime));
        IServiceProvider serviceProvider = (IServiceProvider)app.ApplicationServices.GetService(typeof(IServiceProvider));
        IHostingEnvironment hostingEnv = (IHostingEnvironment)app.ApplicationServices.GetService(typeof(IHostingEnvironment));

        AppProperties properties = new AppProperties(builder.Properties);
        properties.AppName = hostingEnv.ApplicationName;
        properties.OnAppDisposing = lifetime.ApplicationStopping;
        properties.DefaultApp = next;

        configuration(builder);

        return builder.Build<Func<IDictionary<string, object>, Task>>();
    }));
}

协商SignalR服务器的响应:

https://customdomain/signalr/negotiate?clientProtocol=1.5&connectionData = [{&#34; name&#34;:&#34; hubname&#34;}]

{
    "Url": "/signalr",
    "ConnectionToken": "MTY5ODlmZmItMDUxNC00ZmJhLTgzZjMtOTcyOGM5ZTUxY2IwOg==",
    "ConnectionId": "16989ffb-0514-4fba-83f3-9728c9e51cb0",
    "KeepAliveTimeout": 20,
    "DisconnectTimeout": 30,
    "ConnectionTimeout": 110,
    "TryWebSockets": false,
    "ProtocolVersion": "1.5",
    "TransportConnectTimeout": 5,
    "LongPollDelay": 0
}

JS客户端启动:

$.connection.hub.logging = true;
$.connection.hub.start({ 
    transport: ['webSockets', 'longPolling'],
    withCredentials: false
});

完整的Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <httpRuntime targetFramework="4.6"/>
        <compilation targetFramework="4.6" strict="true">           
        </compilation>
    </system.web>
    <system.webServer>
        <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
        </handlers>
        <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
    </system.webServer>
</configuration>

Chrome Tools控制台消息:

[19:05:22 GMT-0400 (Eastern Daylight Time)] SignalR: Fired ajax abort async = false.
[19:05:22 GMT-0400 (Eastern Daylight Time)] SignalR: Auto detected cross domain url.
[19:05:22 GMT-0400 (Eastern Daylight Time)] SignalR: Client subscribed to hub 'concurrentaccesshub'.
[19:05:22 GMT-0400 (Eastern Daylight Time)] SignalR: Negotiating with 'https: //customdomain/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22concurrentaccesshub%22%7D%5D'.
[19:05:22 GMT-0400 (Eastern Daylight Time)] SignalR: longPolling transport starting.
[19:05:23 GMT-0400 (Eastern Daylight Time)] SignalR: Opening long polling request to 'https: //customdomain/signalr/connect?transport=longP…Og%3D%3D&connectionData=%5B%7B%22name%22%3A%22concurrentaccesshub%22%7D%5D'.
[19:05:23 GMT-0400 (Eastern Daylight Time)] SignalR: Long poll complete.
[19:05:23 GMT-0400 (Eastern Daylight Time)] SignalR: LongPolling connected.
[19:05:23 GMT-0400 (Eastern Daylight Time)] SignalR: longPolling transport connected. Initiating start request.

编辑4/26/2017

根据建议,我明确将传输设置为仅仅是webSockets传输:

$.connection.hub.logging = true;
$.connection.hub.start({ 
    transport: ['webSockets'],
    withCredentials: false
});

以下是错误消息:

SignalR: Auto detected cross domain url.
SignalR: Client subscribed to hub 'concurrentaccesshub'.
SignalR: Negotiating with 'http: //localhost:56637/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22concurrentaccesshub%22%7D%5D'.
SignalR: No transports supported by the server were selected.
SignalR: Stopping connection.

1 个答案:

答案 0 :(得分:0)

根据您的描述,我不能在我身边重现这个问题。我可以在Azure Web App上使用Websocket Transport和ASPNET 4.6。

  

“TryWebSockets”:false,

此响应表示在服务器端禁用了webSocket。 webSocket也可以在web.config中禁用。请检查您的Web配置文件是否包含以下部分。

<system.webServer>
  <webSocket enabled="false"/>
</system.webServer>

2017年5月2日更新

由于您使用的是OWIN,请检查您是否已将以下代码添加到Startup.Configure方法以使用网络袜子。

public void Configure(IApplicationBuilder app)
{
    app.UseWebSockets();
    app.UseSignalR();
}