コード例 #1
0
ファイル: ChatHub.cs プロジェクト: ethanharman12/Franglais
        public Guid JoinLobby(string name, string language, Guid? id)
        {
            if (!id.HasValue)
            {
                id = Guid.NewGuid();
            }

            var currentUser = ConnectedUsers.FirstOrDefault(cu => cu.Id == id);

            if (currentUser != null)
            {
                currentUser.Language = language;
                currentUser.UserName = name;

                if (!currentUser.ConnectionIds.Any(cids => cids.Value.Contains(Context.ConnectionId)))
                {
                    if (currentUser.ConnectionIds.Keys.Contains("Lobby"))
                    {
                        currentUser.ConnectionIds["Lobby"].Add(Context.ConnectionId);
                    }
                    else
                    {
                        currentUser.ConnectionIds.Add("Lobby", new List<string>() { Context.ConnectionId });
                    }
                }
            }
            else
            {
                var userModel = new UserModel()
                {
                    Id = id.Value,
                    ConnectionIds = new Dictionary<string, List<string>>(),
                    UserName = name,
                    Language = language,
                    IsChatting = false,
                    Invites = new List<Guid>()
                };

                userModel.ConnectionIds.Add("Lobby", new List<string>() { Context.ConnectionId });
                ConnectedUsers.Add(userModel);
                Clients.OthersInGroup("Lobby").userJoined(userModel);
            }

            Groups.Add(Context.ConnectionId, "Lobby");

            return id.Value;
        }
コード例 #2
0
ファイル: HubTests.cs プロジェクト: ethanharman12/Franglais
        public void JoinLobby()
        {
            UserModel userModel = new UserModel();

            var hub = new ChatHub(new MockTranslator());
            var mockClients = new Mock<IHubCallerConnectionContext<dynamic>>();
            hub.Clients = mockClients.Object;

            var mockGroupManager = new Mock<IGroupManager>();
            hub.Groups = mockGroupManager.Object;

            dynamic all = new ExpandoObject();
            all.userJoined = new Action<UserModel>((model) =>
            {
                userModel = model;
            });

            AttachIdentity(hub, "Tester3", "C3");

            mockClients.Setup(m => m.All).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Caller).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Others).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.OthersInGroup(It.IsAny<string>())).Returns((ExpandoObject)all);

            hub.JoinLobby("Tester3", "French", null);

            Assert.AreEqual(1, ChatHub.ConnectedUsers.Count);
            Assert.AreEqual(false, userModel.IsChatting);
            Assert.AreEqual("French", userModel.Language);
            Assert.AreEqual("Tester3", userModel.UserName);
        }