SignalR HubConnection 启动缓慢

Slow SignalR HubConnection Start

我的问题:有没有办法加快与集线器的初始连接?

详情:

我在自托管 Web 服务 @Win 8.1 中使用 SignalR。 Hub 应用程序获得了本地客户端和远程客户端。

我通过 DNS、localhost 和 127.0.0.1 授予 SignalR 访问权限:

'netsh http add urlacl url=http://<Replace>:<Port>/ user=Everyone'

通常所有客户端甚至本地客户端都使用 DNS。并且有效。

我的问题是一个生成的子进程(使用 c# Microsoft.AspNet.SignalR.Client.HubConnection)。它通常在 400 毫秒内连接。没关系。但有时需要几秒钟。

我已尝试在此客户端切换到 127.0.0.1 和本地主机,但没有任何更改。

初始连接后,SignalR 速度很快。

如果没有简单的方法,我必须切换回纯 UDP。

集线器的 SignalR 配置:

using System;
using System.Web.Http;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.StaticFiles;
using Owin;

[assembly: OwinStartup(typeof(SignalRStartUp))]
namespace Onsite
{
    public class SignalRStartUp
    {
        // Any connection or hub wire up and configuration should go here
        public void Configuration(IAppBuilder pApp)
        {
            try
            {
                pApp.UseCors(CorsOptions.AllowAll);
                pApp.MapSignalR();
                pApp.UseFileServer(true);

                var lOptions = new StaticFileOptions
                {
                    ContentTypeProvider = new CustomContentTypeProvider(),
                    FileSystem = new PhysicalFileSystem(Constants.Root)
                };

                pApp.UseStaticFiles(lOptions);

                // Configure Web API for self-host. 
                var lConfig = new HttpConfiguration();
                lConfig.Routes.MapHttpRoute("RemoteApi", "api/{controller}/{action}");
                lConfig.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });
                pApp.UseWebApi(lConfig); 
            }
            catch (Exception lEx)
            {
                Logger.Error(lEx);
            }
        }
    }
}

客户端启动:

public static string GetConnectionString(string pHost = null)
{
    var lHost = pHost ?? GetMainClientDns();
    return string.Format("http://{0}{1}", lHost, SignalRPort);
}

private void StartSignalR()
{
    try
    {
        var lConnectionString = GetConnectionString("127.0.0.1");

        var lStopWatch = new Stopwatch();
        lStopWatch.Restart();
        IsConnectingHost = true;

        _connection = new HubConnection(lConnectionString, string.Format("AccessKey={0}&Role={0}", Constants.AccessKeyPlugIn));
        _connection.Reconnected += SetConnected;
        _connection.Reconnecting += SetDisConnected;

        MTalkHub = _connection.CreateHubProxy("OnsiteHub");

        MTalkHub.On("RequestSetNext", RequestSetNext);
        MTalkHub.On("RequestSetPrevious", RequestSetPrevious);
        MTalkHub.On("RequestEcho", RequestEcho);

        _connection.TransportConnectTimeout = _transportConnectTimeout;

        var lTask = _connection.Start();
        lTask.Wait();

        lStopWatch.Stop();

        SetConnected();
     }
    catch (TargetInvocationException lEx)
    {
        IsDisconnected = true;
        Task.Run(() => TryToConnect());
        Logger.Fatal(string.Format("Server failed to start. Already running on: '{0}'", lConnectionString), lEx);
    }
    catch (Exception lEx)
    {
        IsDisconnected = true;
        Task.Run(() => TryToConnect());
        Logger.Fatal(string.Format("Connecting to: '{0}' failed!", lConnectionString.ToStringNs()));
    }
    finally
    {
        IsConnectingHost = false;
    }
}

修复另一个问题后,我的长初始连接也消失了。

我发现了什么:存在竞争条件,很少通过网络共享锁定 FileAccess 通过更新本地缓存的缩略图来阻止集线器。

谢谢!