2014-03-26 47 views
2

我看着這個excellent SO post,但它沒有給我足夠的細節。我想存儲有關SignalR Hub的所有信息 - 它是連接,狀態,關閉應用程序的用戶,以及在應用程序啓動時檢索的。如果我們通過SignalR請求數據,然後我們將網站發佈到網站,我們遇到的問題似乎會失去聯繫。如何在應用程序關閉/啓動時保存/恢復SignalR Hub,連接,狀態

這是我現在正在做的。在OnDisconnected:

var connection = db.SignalRConnections.Find(Context.ConnectionId); 
        connection.Connected = false; 
        db.SaveChanges(); 

在OnConnected:

public override Task OnConnected() 
     { 
      try 
      { 
       var name = Context.User.Identity.Name; 

       using (savitasEntities2 entities = new savitasEntities2()) 
       { 
        var user = entities.SignalRUsers 
         .Include(u => u.SignalRConnections) 
         .SingleOrDefault(u => u.UserName == name); 

        if (user == null) 
        { 
         user = new SignalRUser 
         { 
          UserName = name, 
          SignalRConnections = new List<SignalRConnection>() 
         }; 
         entities.SignalRUsers.Add(user); 
        } 

        user.SignalRConnections.Add(new SignalRConnection 
        { 
         ConnectionID = Context.ConnectionId, 
         UserAgent = Context.Request.Headers["User-Agent"], 
         Connected = true 
        }); 
        entities.SaveChanges(); 
       } 

      } 

這是兩個表中的DB:

public partial class SignalRConnection 
    { 
     public string ConnectionID { get; set; } 
     public string UserAgent { get; set; } 
     public bool Connected { get; set; } 
     public string SignalRUser_UserName { get; set; } 

     public virtual SignalRUser SignalRUser { get; set; } 
    } 

public partial class SignalRUser 
    { 
     public SignalRUser() 
     { 
      this.SignalRConnections = new HashSet<SignalRConnection>(); 
     } 

     public string UserName { get; set; } 

     public virtual ICollection<SignalRConnection> SignalRConnections { get; set; } 
    } 
} 

我應該做的啓動點別的/ Global.asax中關閉?我也有一個線程安全字典來路由我們的數據:

public static ConcurrentDictionary<string, DataSet> publicDataDictionary = new ConcurrentDictionary<string, DataSet>(); 

應該得到序列化嗎?謝謝!

回答

0
var connection = db.SignalRConnections.Find(Context.ConnectionId); 
       connection.Connected = false; 
       db.SaveChanges(); 

而且OnConnected:

public override Task OnConnected() 
    { 
     try 
     { 
      var name = Context.User.Identity.Name; 

      using (savitasEntities2 entities = new savitasEntities2()) 
      { 
       var user = entities.SignalRUsers 
        .Include(u => u.SignalRConnections) 
        .SingleOrDefault(u => u.UserName == name); 

       if (user == null) 
       { 
        user = new SignalRUser 
        { 
         UserName = name, 
         SignalRConnections = new List<SignalRConnection>() 
        }; 
        entities.SignalRUsers.Add(user); 
       } 

       user.SignalRConnections.Add(new SignalRConnection 
       { 
        ConnectionID = Context.ConnectionId, 
        UserAgent = Context.Request.Headers["User-Agent"], 
        Connected = true 
       }); 
       entities.SaveChanges(); 
      } 

     } 

的數據庫表:

public partial class SignalRConnection 
    { 
     public string ConnectionID { get; set; } 
     public string UserAgent { get; set; } 
     public bool Connected { get; set; } 
     public string SignalRUser_UserName { get; set; } 

     public virtual SignalRUser SignalRUser { get; set; } 
    } 
public partial class SignalRUser 
    { 
    public SignalRUser() 
    { 
     this.SignalRConnections = new HashSet<SignalRConnection>(); 
    } 

    public string UserName { get; set; } 

    public virtual ICollection<SignalRConnection> SignalRConnections { get; set; } 
} 
} 

我可能做的時候在Global.asax中東西就啓動/關機別的嗎?

相關問題