Пример #1
0
        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 });
            }
        }
Пример #2
0
        public static void PutUsersOffline()
        {
            using (var db = new ZigChatContext())
            {
                foreach (var connection in db.Connections)
                {
                    connection.IsOnline = false;
                }

                db.SaveChanges();
            }
        }
Пример #3
0
        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());
        }