Пример #1
0
        public async Task SendMessage(int roomId, ChatMessage mess)
        {
            var room = ChatRooms.FirstOrDefault(rm => rm.Id == roomId);

            if (room != null)
            {
                var sender = room.Users.First(cu => cu.ConnectionIds["Room" + roomId].Contains(Context.ConnectionId));

                mess.Sender = sender;
                mess.Id = messageId++;

                var sourceLang = mess.Sender.Language.Substring(0, 2);

                //should just group by language in case of >2 users
                foreach (var user in room.Users.Where(u => u != sender))
                {
                    var userLang = user.Language.Substring(0, 2);

                    if (sourceLang != userLang)
                    {
                        mess.Translation = await translator.TranslateMessage(mess.Message, sourceLang, userLang);
                    }
                    mess.ServerSent = DateTime.Now;

                    Clients.Clients(user.ConnectionIds["Room" + roomId].ToList()).receiveMessage(mess);
                }
                Clients.Clients(sender.ConnectionIds["Room" + roomId]).receiveMessage(mess);
            }
        }
Пример #2
0
        public void SendMessage()
        {
            var guid1 = Guid.NewGuid();
            var guid2 = Guid.NewGuid();

            ChatHub.ConnectedUsers.Add(new UserModel()
            {
                Id = guid1,
                Language = "English",
                UserName = "******",
                IsChatting = false,
                Invites = new List<Guid>(),
                ConnectionIds = new Dictionary<string, List<string>>() { { "Room2", new List<string>() { "C9" } } }
            });
            ChatHub.ConnectedUsers.Add(new UserModel()
            {
                Id = guid2,
                Language = "French",
                UserName = "******",
                IsChatting = false,
                Invites = new List<Guid>(),
                ConnectionIds = new Dictionary<string, List<string>>() { { "Room2", new List<string>() { "C10" } } }
            });

            ChatRoom chatRoom = new ChatRoom() { Id = 2, Users = ChatHub.ConnectedUsers.ToList() };
            ChatHub.ChatRooms.Add(chatRoom);

            ChatMessage mess = new ChatMessage();

            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.receiveMessage = new Action<ChatMessage>((message) =>
            {
                mess = message;
            });

            AttachIdentity(hub, "Tester9", "C9");

            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);
            mockClients.Setup(m => m.User(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Client(It.IsAny<string>())).Returns((ExpandoObject)all);
            mockClients.Setup(m => m.Clients(It.IsAny<List<string>>())).Returns((ExpandoObject)all);

            hub.SendMessage(2, new ChatMessage()
            {
                ClientSent = DateTime.Now,
                Message = "Hello World",
                Sender = chatRoom.Users[0]
            }).Wait();

            Assert.AreEqual(1, mess.Id);
            Assert.AreEqual("Tester9", mess.Sender.UserName);

            Assert.IsNotNull(mess.ServerSent);
            Assert.AreNotEqual(DateTime.MinValue, mess.ServerSent);
            Assert.IsTrue(mess.ServerSent > mess.ClientSent);

            Assert.AreEqual("English", mess.Sender.Language);
            Assert.AreEqual("Hello World", mess.Message);
            Assert.AreEqual("French: Hello World", mess.Translation);
        }