2

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

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

稍微介绍一下我们的配置:

  • SignalR 服务器:
    • “Microsoft.AspNet.SignalR”:“2.2.1”
    • 框架:net462
  • 主办:
    • Azure 应用服务
      • 网络套接字:开
      • CORS:允许的来源:*
      • 定价层:基本:1 小
      • .NET 框架版本 v4.6
      • SSL证书

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

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

启动.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=[{"name":"hubname"}]

{
    "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 工具控制台消息:

[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.

编辑 2017 年 4 月 26 日

根据建议,我明确地将传输设置为仅 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.
4

1 回答 1

0

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

“TryWebSockets”:假,

这个响应意味着 webSocket 在服务器端被禁用。webSocket 也可以在 web.config 中禁用。请检查您的 Web 配置文件是否包含以下部分。

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

2017 年 5 月 2 日更新

由于您使用的是 OWIN,请检查您是否在 Startup.Configure 方法中添加了以下代码以使用 web sock。

public void Configure(IApplicationBuilder app)
{
    app.UseWebSockets();
    app.UseSignalR();
}
于 2017-04-22T05:35:23.690 回答