Esempio n. 1
0
        private void HandleLoginMsg(Login login, TalkBackDBContext dbContext)
        {
            UsersRepository repo = new UsersRepository(dbContext);

            // check if User Exists:
            if (repo.IsUserExist(login.Name))
            {
                // check password:
                if (!repo.IsPasswordCorrect(login.Name, login.Password))
                {
                    LoginResponse response = new LoginResponse {
                        Success = false, ErrorMessage = "Wrong password, please try again"
                    };
                    WebSocketMiddleware.SendStringAsync(_webSocket, response.ToXml().ToString());
                    return;
                }
            }
            else
            {
                // new user: add it to repo
                repo.AddNewUser(new UserModel {
                    Name = login.Name, Password = login.Password
                });
            }

            // login successfully:
            // 1. record UserName for this handler
            UserName = login.Name;
            // 2. add to the websocket dictionary
            WebSocketMiddleware._sockets.TryAdd(UserName, _webSocket);
            // 3. inform all online users about the new/updated contact,
            //    create a Set<string> of online users:
            ISet <string> online = new HashSet <string>();
            Contact       me     = new Contact {
                Name = UserName, IsOnline = true
            };
            string contactUpdate = me.ToXml().ToString();

            online = Broadcast(contactUpdate);
            // 4. send the client the list of contacts:
            IList <AbstractXmlSerializable> contacts = repo.GetContactsList(UserName, online);
            string contactsList = ModelXmlMapper.GetAsXmlString(contacts);

            WebSocketMiddleware.SendStringAsync(_webSocket, contactsList);
        }
Esempio n. 2
0
 public async void SendListAsync(IList <AbstractXmlSerializable> list)
 {
     string serialized = ModelXmlMapper.GetAsXmlString(list);
     await connectTask.ContinueWith(_ => SendMessageUsingMessageWebSocketAsync(serialized));
 }
Esempio n. 3
0
 public async void SendObjectAsync(AbstractXmlSerializable obj)
 {
     string serialized = ModelXmlMapper.GetAsXmlString(obj);
     await connectTask.ContinueWith(_ => SendMessageUsingMessageWebSocketAsync(serialized));
 }