Example #1
0
        public void JoinCallsAddUserIfValidUserIdInCookieAndUserList()
        {
            var repository = new ChatRepository();
            var user = new ChatUser
            {
                Id = "1234",
                Name = "John",
                Hash = "Hash"
            };
            repository.Users.Add(user);

            var chat = new ChatHub(repository);
            var connection = new Mock<IConnection>();
            var prinicipal = new Mock<IPrincipal>();
            var cookies = new HttpCookieCollection();
            cookies.Add(new HttpCookie("userid", "1234"));
            var clientState = new TrackingDictionary();
            string clientId = "20";
            chat.Agent = new ClientAgent(connection.Object, "Chat");
            chat.Caller = new SignalAgent(connection.Object, clientId, "Chat", clientState);
            chat.Context = new HubContext(clientId, cookies, prinicipal.Object);

            chat.Join();

            Assert.Equal("1234", clientState["id"]);
            Assert.Equal("John", clientState["name"]);
            Assert.Equal("Hash", clientState["hash"]);
            Assert.Equal("20", user.ClientId);
            // Need a better way to verify method name and arguments
            connection.Verify(m => m.Broadcast("Chat.20", It.IsAny<object>()), Times.Once());
        }
Example #2
0
        public void JoinReturnsFalseIfNoCookies()
        {
            var repository = new ChatRepository();
            var chat = new ChatHub(repository);
            var connection = new Mock<IConnection>();
            var prinicipal = new Mock<IPrincipal>();
            var clientState = new TrackingDictionary();
            string clientId = "test";
            chat.Agent = new ClientAgent(connection.Object, "Chat");
            chat.Caller = new SignalAgent(connection.Object, clientId, "Chat", clientState);
            chat.Context = new HubContext(clientId, new HttpCookieCollection(), prinicipal.Object);

            bool result = chat.Join();
            string versionString = typeof(ChatHub).Assembly.GetName().Version.ToString();

            Assert.Equal(versionString, clientState["version"]);
            Assert.False(result);
        }
Example #3
0
 public Chat(ChatRepository db)
 {
     _db = db;
 }
Example #4
0
        private static void Sweep(ChatRepository db)
        {
            if (_sweeping)
            {
                return;
            }

            _sweeping = true;

            var clients = GetClients<Chat>();

            var inactiveUsers = new List<ChatUser>();

            foreach (var user in db.Users)
            {
                var elapsed = DateTime.UtcNow - user.LastActivity;
                if (elapsed.TotalMinutes > 5)
                {
                    user.Active = false;
                    inactiveUsers.Add(user);
                }
            }

            var roomGroups = from u in inactiveUsers
                             from r in u.Rooms
                             select new { User = u, Room = r } into tuple
                             group tuple by tuple.Room into g
                             select new
                             {
                                 Room = g.Key,
                                 Users = g.Select(t => new UserViewModel(t.User))
                             };

            foreach (var roomGroup in roomGroups)
            {
                clients[roomGroup.Room.Name].markInactive(roomGroup.Users).Wait();
            }

            _sweeping = false;
        }