Пример #1
0
        public void AddUser(Client c)
        {
            ConnectedUsers.Add(c);

            Clients.All.updateUsers(ConnectedUsers.Count(), ConnectedUsers.Select(u => u.NickName));

            using (var db = new AppDbContext())
            {
                var user = new User()
                {
                    NickName = c.NickName
                };
                db.Users.Add(user);
                db.SaveChanges();
            }
        }
Пример #2
0
        /// <summary>
        /// To be called when a new User Connects, Notifies other users that a new User is Connected
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public void Connect(string name)
        {
            var id = Context.ConnectionId;

            if (ConnectedUsers == null)
            {
                ConnectedUsers = new HashSet <iUser>();
            }
            if (ConnectedUsers.Count(x => x.ID == id) == 0)
            {
                ConnectedUsers.Add(new iUser()
                {
                    ID = id, Name = name
                });
                Clients.Caller.onConnected(id, name, ConnectedUsers.Where(x => x.ID != id));
                Clients.AllExcept(id).onNewUserConnected(id, name);
            }
        }
Пример #3
0
        private void ConnectToClient()
        {
            Console.WriteLine("Connected clients:");

            for (int i = 0; i < ConnectedUsers.Count(); i++)
            {
                Console.WriteLine($"{i}: {ConnectedUsers[i]}");
            }

            Console.WriteLine("Please select client:");

            var idS = Console.ReadLine();

            if (!_messenger.IsConnected)
            {
                var id = Convert.ToInt32(idS);

                _messenger.Connect(ConnectedUsers[id]);
            }
        }
Пример #4
0
 public override Task OnConnected()
 {
     connectedUsers.AddUser(new User(Context.ConnectionId));
     return(Clients.All.populateNumUsers(connectedUsers.Count()));
 }
Пример #5
0
        private void ClientLoop(object o)
        {
            Socket temp_client = ((IDSocket)o).Handle_Socket;
            User   client_user = new User(((IDSocket)o).Handle_ID, null, null);

            try
            {
                while (true)
                {
                    Request client_request = DataTransferHelper.GetRequestFromClient(temp_client);

                    switch (client_request.RequestType)
                    {
                    case "registration":
                    {
                        RegistrationRequest registrationRequest = client_request as RegistrationRequest;

                        if (!IsUserNameExists(registrationRequest.UserName))
                        {
                            AddUser(new User(GetNewUserID(), registrationRequest.UserName, registrationRequest.Password));
                            RegistrationResponse registrationResponse = new RegistrationResponse();
                            DataTransferHelper.SendResponseToClient(temp_client, registrationResponse);
                        }
                        else
                        {
                            RegistrationResponse registrationResponse = new RegistrationResponse()
                            {
                                Ok    = false,
                                Error = "user_name_exists"
                            };
                            DataTransferHelper.SendResponseToClient(temp_client, registrationResponse);
                        }

                        break;
                    }

                    case "auth":
                    {
                        AuthRequest authRequest = client_request as AuthRequest;

                        if (IsUserNameExists(authRequest.UserName))
                        {
                            User temp_user = GetUserByName(authRequest.UserName);
                            if (authRequest.Password == temp_user.Password)
                            {
                                if (IsBannedUser(temp_user.ID))
                                {
                                    BanResponse banResponse = new BanResponse();
                                    DataTransferHelper.SendResponseToClient(temp_client, banResponse);
                                }
                                else
                                {
                                    if (IsConnectedUser(temp_user.ID))
                                    {
                                        AuthResponse authResponse = new AuthResponse(temp_user);
                                        authResponse.Error = "user_is_connected";
                                        authResponse.Ok    = false;
                                        DataTransferHelper.SendResponseToClient(temp_client, authResponse);
                                    }
                                    else
                                    {
                                        AuthResponse authResponse = new AuthResponse(temp_user);
                                        DataTransferHelper.SendResponseToClient(temp_client, authResponse);
                                        temp_user.IsConnected = true;
                                        RaisePropertyChangedEvent("UserDataBase");
                                        RaisePropertyChangedEvent("ConnectedUsers");
                                        _clients.Add(temp_user.ID, temp_client);
                                        _clients.Remove(client_user.ID);
                                        client_user.ID       = temp_user.ID;
                                        client_user.Name     = temp_user.Name;
                                        client_user.Password = temp_user.Password;

                                        for (int i = 0; i < ConnectedUsers.Count(); i++)
                                        {
                                            if (ConnectedUsers.ElementAt(i).ID == client_user.ID)
                                            {
                                                continue;
                                            }
                                            UserSignInResponse userSignInResponse = new UserSignInResponse(ConnectedUsers.ElementAt(i));
                                            DataTransferHelper.SendResponseToClient(temp_client, userSignInResponse);
                                        }
                                        for (int i = 0; i < _clients.Count(); i++)
                                        {
                                            if (_clients.ElementAt(i).Key == client_user.ID)
                                            {
                                                continue;
                                            }
                                            UserSignInResponse userSignInResponse = new UserSignInResponse(client_user);
                                            DataTransferHelper.SendResponseToClient(_clients.ElementAt(i).Value, userSignInResponse);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                AuthResponse authResponse = new AuthResponse(null)
                                {
                                    Ok    = false,
                                    Error = "invalid_password"
                                };
                                DataTransferHelper.SendResponseToClient(temp_client, authResponse);
                            }
                        }
                        else
                        {
                            AuthResponse authResponse = new AuthResponse(null)
                            {
                                Ok    = false,
                                Error = "user_name_not_exists"
                            };
                            DataTransferHelper.SendResponseToClient(temp_client, authResponse);
                        }

                        break;
                    }

                    case "signout":
                    {
                        GetUserByID(client_user.ID).IsConnected = false;
                        throw new Exception();
                    }

                    case "message":
                    {
                        MessageResponse messageResponse = new MessageResponse((client_request as MessageRequest).Message);
                        SendResponseToAllClients(messageResponse);
                        break;
                    }

                    default:
                        break;
                    }
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                temp_client?.Close();
                _clients.Remove(client_user.ID);
                if (client_user != null)
                {
                    User temp_user = GetUserByID(client_user.ID);
                    if (temp_user != null)
                    {
                        temp_user.IsConnected = false;
                    }
                    UserSignOutResponse userSignOutResponse = new UserSignOutResponse(client_user);
                    SendResponseToAllClients(userSignOutResponse);
                }
                RaisePropertyChangedEvent("UserDataBase");
                RaisePropertyChangedEvent("ConnectedUsers");
            }
        }