如果新浏览器打开,则与旧浏览器的SignalR连接将断开

时间:2014-04-02 22:25:50

标签: c# asp.net-membership signalr

我有应用程序,我使用SignalR显示来自传感器的数据。它使用ASP.net成员身份来验证用户。如果我只打开一个浏览器窗口(例如Firefox),一切正常。如果我在另一个浏览器中打开同一网站,即使用户不同,Chrome同时与firefox浏览器的信号连接也会下降。这就是我用来广播消息的内容:

集线器

[Authorize]
public class DataHub:Hub
{
     private readonly RealTimeData _sensor;

    public DataHub() : this(RealTimeData.Instance) { }

    public DataHub(RealTimeData data)
    {
       _sensor = data;
    }   

    public override Task OnConnected()
    {
       // _sensor.UserId = Context.ConnectionId; changed to
        _sensor.UserId = Membership.GetUser().ProviderUserKey.ToString();
        return base.OnConnected();
    }
}
public class RealTimeData
{
   //User Id
    public String UserId { get; set; }
   private readonly static Lazy<RealTimeData> _instance = new Lazy<RealTimeData>(() => new RealTimeData(GlobalHost.ConnectionManager.GetHubContext<DataHub>().Clients));// Singleton instance
    private IHubConnectionContext Clients;
    private void BroadcastDataOfAllSensors(List<SensorDetails> sensor)
    { 
            //Clients.Client(UserId).updateDashboard(sensor);changed to
              Clients.User(UserId).updateDashboard(sensor);      
    }
}

应用程序启动

public class StartUp
{
    public void Configuration(IAppBuilder app)
    {
        var idProvider = new UserIdProvider();
        GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => idProvider);
        app.MapSignalR();
    }
}

用户ID

public class UserIdProvider : IUserIdProvider
{
    public string GetUserId(IRequest request)
    {
        var userId = Membership.GetUser().ProviderUserKey;
        return userId.ToString();
    }
}

0 个答案:

没有答案