コード例 #1
0
        public void RequestActivation(string json)
        {
            string request = string.Empty;

            if (json != string.Empty)
            {
                var key = (JObject)JsonConvert.DeserializeObject(json);
                request = key["Key"].Value <string>();
            }
            switch (request)
            {
            case "registration":
                RegistrationJSON registrationJSON = System.Text.Json.JsonSerializer.Deserialize <RegistrationJSON>(json);
                Console.WriteLine($"New Client. Registration request.");
                Console.WriteLine("Requests: " + json);
                Registration(registrationJSON);
                Server.SendUpdateOnline();
                break;

            case "emailconfirmation":
                EmailConfirmationJSON emailConfirmationJSON = System.Text.Json.JsonSerializer.Deserialize <EmailConfirmationJSON>(json);
                Console.WriteLine($"New Client. Email confirmation request.");
                Console.WriteLine("=> Requests: " + json);
                EmailCodeVerification(emailConfirmationJSON);
                break;

            case "authorization":
                AuthorizationJSON authorizationJSON = System.Text.Json.JsonSerializer.Deserialize <AuthorizationJSON>(json);
                Console.WriteLine($"New Client. Authorization request.");
                Console.WriteLine("=> Requests: " + json);
                Authorization(authorizationJSON);
                Server.SendUpdateOnline();
                break;

            case "addfriend":
                AddFriendJSON addFriendJSON = System.Text.Json.JsonSerializer.Deserialize <AddFriendJSON>(json);
                Console.WriteLine($"New Client. Username: {Client.Username} add friend username:{addFriendJSON.FriendUsername}");
                Console.WriteLine("=> Requests: " + json);
                AddFriend(addFriendJSON);
                Server.SendUpdateOnline();
                break;

            case "getfriendlist":
                GetFrindListJSON getFrindListJSON = System.Text.Json.JsonSerializer.Deserialize <GetFrindListJSON>(json);
                Console.WriteLine($"Client Username: {Client.Username}. FriendList request.");
                Console.WriteLine("=> Requests: " + json);
                GetFriendList(getFrindListJSON);
                break;

            case "getchat":
                GetChatJSON getChatJSON = System.Text.Json.JsonSerializer.Deserialize <GetChatJSON>(json);
                Console.WriteLine($"Client Username: {Client.Username}. GetChat request.");
                Console.WriteLine("=> Requests: " + json);
                GetChat(getChatJSON);
                break;

            case "sendmessage":
                SendMessageJSON sendMessageJSON = System.Text.Json.JsonSerializer.Deserialize <SendMessageJSON>(json);
                Console.WriteLine($"Client Username: {Client.Username}. Send message to user {sendMessageJSON.FriendUsername}");
                Console.WriteLine("=> Requests: " + json);
                SendMessage(sendMessageJSON);
                break;

            case "removefriend":
                AddFriendJSON removeFriend = System.Text.Json.JsonSerializer.Deserialize <AddFriendJSON>(json);
                Console.WriteLine($"Client Username: {Client.Username}. Remove friend {removeFriend.FriendUsername}");
                Console.WriteLine("=> Requests: " + json);
                RemoveFriend(removeFriend);
                Server.SendUpdateOnline();
                break;

            case "statusonline":
                StatusOnlineJSON statusOnlineJSON = System.Text.Json.JsonSerializer.Deserialize <StatusOnlineJSON>(json);
                Console.WriteLine($"Client Username: {Client.Username}. Refresh status online.");
                Console.WriteLine("=> Requests: " + json);
                SendOlineStatus(statusOnlineJSON);
                break;

            default:
                Console.WriteLine($"Client IP: {Client.IPClient}. Sent string.Empty request.");
                Client.Close();
                break;
            }
        }
コード例 #2
0
        private void Registration(RegistrationJSON registrationJSON)
        {
            bool   verification = false;
            string reason       = "Сервер разрешает регистрацию.";

            try
            {
                using (StreamReader sr = new StreamReader(ServerDirectory.Database_path, Encoding.UTF8))
                {
                    string line; //Сделать оптимизацию ввиде брейка
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line == string.Empty)
                        {
                            continue;
                        }
                        if (line == registrationJSON.Username)
                        {
                            reason       = "Такой никнейм уже существует.";
                            verification = true;
                            break;
                        }
                        if ((line = sr.ReadLine()) == registrationJSON.Email)
                        {
                            reason       = "Такая электроная почта уже зарегистрирована.";
                            verification = true;
                            break;
                        }
                        line = sr.ReadLine();
                    }
                    sr.Close();
                    sr.Dispose();
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to open database.");
            }
            if (!verification)
            {
                Console.WriteLine($"Client ID: {Client.Id}. User: {registrationJSON.Username}, Email: {registrationJSON.Email}. Registered on the server.");
                Client.SetDataUser(registrationJSON.Username, registrationJSON.Email, registrationJSON.Password);

                AllowJSON allow = new AllowJSON()
                {
                    Allow = true, Reason = reason
                };
                string allowJSON = System.Text.Json.JsonSerializer.Serialize <AllowJSON>(allow);
                Client.SendAnswer(allowJSON);
                SendCodeEmail(registrationJSON.Email, registrationJSON.Username);
            }
            else
            {
                Console.WriteLine($"Client ID: {Client.Id}. User: {registrationJSON.Username}, Email: {registrationJSON.Email}. Can't register.");

                AllowJSON allow = new AllowJSON()
                {
                    Allow = false, Reason = reason
                };
                string allowJSON = System.Text.Json.JsonSerializer.Serialize <AllowJSON>(allow);
                Client.SendAnswer(allowJSON);
            }
        }