public object ConnectUser(string userName) { try { using (var db = new ZigChatContext()) { // Check if there if a connection for the specified user name have ever been made var existingConnection = db.Connections.Where(x => x.UserName.ToLower() == userName.ToLower()).SingleOrDefault(); if (existingConnection != null) { // If there's an old connection only the connection id and the online status are changed. existingConnection.ConnectionId = Context.ConnectionId; existingConnection.IsOnline = true; } else { // If not, then a new connection is created db.Connections.Add(new Connection { ConnectionId = Context.ConnectionId, UserName = userName, IsOnline = true }); } db.SaveChanges(); } UsersOnline(); return(new { Success = true }); } catch (Exception ex) { return(new { Success = false, ErrorMessage = ex.Message }); } }
public static void PutUsersOffline() { using (var db = new ZigChatContext()) { foreach (var connection in db.Connections) { connection.IsOnline = false; } db.SaveChanges(); } }
public override Task OnReconnected() { using (var db = new ZigChatContext()) { var connection = db.Connections.Where(x => x.ConnectionId == Context.ConnectionId).SingleOrDefault(); if (connection == null) { throw new Exception("An attempt to reconnect a non tracked connection id have been made."); } connection.IsOnline = true; db.SaveChanges(); } UsersOnline(); return(base.OnReconnected()); }