Example #1
0
        /// Method that creates chat Form
        public ChatSessionForm SwitchToChat()
        {
            Visible = false;

            var sessionForm = new ChatSessionForm(this);

            sessionForm.Show();

            return(sessionForm);
        }
Example #2
0
        //Вход и автоматическая регистрация в случае нового юзера
        public void SignIn(string name, string login, string password)
        {
            if (Client == null)
            {
                return;
            }

            if (!IsConnected())
            {
                CrashAndReport(new Exception("Connection is lost. Attempting to reconnect..."));
                loginForm.BeginInvoke(NewChatSession);
                Dispose();
                return;
            }

            var authorizationData = new UserData()
            {
                Name     = name,
                Login    = login,
                Password = password,
            };

            SendMessage(authorizationData);

            //or Environment.TickCount?
            var counter = 0;

            while (!Stream.DataAvailable && counter < 250)
            {
                counter++;
                Thread.Sleep(20);
            }

            if (Stream.DataAvailable)
            {
                var message = ReceiveMessage();

                if (message?.GetType() == typeof(TextMessage) && (message as TextMessage).Text == "Access granted!")
                {
                    loginForm.BeginInvoke(UpdateView, "Access granted!", Color.Blue);
                    Thread.Sleep(350);

                    var form = loginForm.Invoke(SwitchToChat);
                    sessionForm = (ChatSessionForm)form;

                    UpdateChat       = sessionForm.UpdateChat;
                    UpdateOnlineList = sessionForm.UpdateOnlineList;

                    ListenThread = new Thread(Listen);
                    ListenThread.Start();
                }

                else
                {
                    loginForm.BeginInvoke(UpdateView, "Wrong password or username", Color.Red);
                }
            }

            if (counter >= 250)
            {
                loginForm.BeginInvoke(UpdateView, "Message receive problem.", Color.Red);
            }
        }