azure - SignalR 不使用 ASPNET 4.6 的 Websocket 传输

标签 azure websocket signalr asp.net-4.6

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

总的来说,我很难找到有关让 SignalR 使用 WebSocket 进行协商的要求的最新信息。有人可以帮助我们找出为什么没有使用 WebSocket 吗?

关于我们的配置的一些信息:

  • SignalR 服务器:
    • “Microsoft.AspNet.SignalR”:“2.2.1”
    • 框架:net462
  • 托管:
    • Azure 应用服务
      • 网络套接字:开启
      • CORS:允许的来源:*
      • 定价等级:基本:1 小
      • .NET Framework 版本 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.

最佳答案

根据您的描述,我无法重现该问题。我可以在 Azure Web App 上将 Websocket 传输与 ASPNET 4.6 结合使用。

"TryWebSockets": false,

此响应意味着 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();
}

关于azure - SignalR 不使用 ASPNET 4.6 的 Websocket 传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43553504/

相关文章:

wcf - 在开发计算机上托管 azure appfabric 服务总线示例

Azure管道错误: Could not find any file matching the template file pattern

python - WebSocket JWT Token 连接授权

javascript - PhoneGap for iOS 中的 websockets 状态如何?

SignalR:如何避免意外页面刷新

azure - 将现有逻辑应用模板导入 Visual Studio Azure 资源组项目

azure - Azure 逻辑应用是否支持 JSON 架构验证中的 oneOf、anyOf

javascript - 如何从客户端 Angular 处理 CQRS

c#-4.0 - SignalR 是 Microsoft 独有的技术吗?

asp.net-mvc - SignalR - 使用 Windows 和匿名身份验证时连接 ID 的格式不正确