Slow SignalR HubConnection Start
本文关键字:Start HubConnection SignalR Slow | 更新日期: 2023-09-27 18:06:25
我的问题:是否有一种方法可以加速到集线器的初始连接?
细节:
我在一个自托管的Webservice @Win 8.1中使用SignalR。Hub应用程序有本地客户端和远程客户端。
我通过DNS、localhost和127.0.0.1授予SignalR访问权限:
'netsh http add urlacl url=http://<Replace>:<Port>/ user=Everyone'
通常所有客户端甚至本地客户端都在使用DNS。
我的问题是一个sporned子进程(使用c# microt.aspnet . signalr . client . hubconnection)。它通常在400毫秒内连接。这是好的。但有时只需要几秒钟。
我已经尝试在这个客户端切换到127.0.0.1和本地主机,但没有任何改变。
初始连接之后,SignalR非常快。
如果没有简单的方法,我必须切换回普通UDP。
SignalR Config at Hub:
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锁通过更新本地缓存的缩略图阻塞了Hub。
谢谢!