Example #1
0
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///


        public void Receiving2(IWindowsViewModel windowsViewModel,
                               IMessageContent messageContent, IJsonContainer container)
        {
            _windowsViewModel = windowsViewModel;

            FirstTime = true;

            IsConnected = true;

            var uiContext = SynchronizationContext.Current;

            Task.Run(() =>
            {
                if (GetPermission(uiContext, container))///true then next
                {
                    _windowsViewModel.ChangeView(2);

                    //while (IsConnected)
                    //{
                    Chat(uiContext, container, messageContent);
                    //}
                }
                else
                {
                    //FirstTimeLogin = false;//to isConnected class ?

                    _windowsViewModel.ChangeView(1);

                    // here some static error class witch binging to label under login panel to show message!
                }
            });
        }
Example #2
0
        private StringBuilder ReceivData(SynchronizationContext uiContext, IJsonContainer container)
        {
            var buffer = new byte[256];

            int size = 0;

            var textFromServer = new StringBuilder();

            do
            {
                try
                {
                    size = TcpSocket.tcpSocket.Receive(buffer);
                }
                catch (Exception ex)
                {
                    AlertMessages.Show(ex.Message);

                    uiContext.Send(x => OnlineUsers.UserList.Clear(), null);

                    container?.Persons?.Clear();

                    TcpSocket.tcpSocket.Shutdown(SocketShutdown.Both);

                    TcpSocket.tcpSocket.BeginDisconnect(true, new AsyncCallback(DisconnectCallback), TcpSocket.tcpSocket);


                    IsConnected = false;

                    _windowsViewModel.ChangeView(1);
                }


                textFromServer.Append(Encoding.UTF8.GetString(buffer, 0, size));
            } while (TcpSocket.tcpSocket.Available > 0);

            return(textFromServer);
        }
        public void Send()
        {
            _messageContent.MessageText   = SendText;
            _messageContent.MessageTime   = DateTime.Now.ToShortTimeString();
            _messageContent.MessageColour = SenderReceiwer.Send;

            if (!_messageContent.PictureChanged)
            {
                _messageContent.MessagePicture = UserGender.GetUserGender();
            }

            _messageContent.Name = UserInfo.Name;

            _messageContent.Id = User.Id;

            if (UserInfo.AddedPicture != null)
            {
                _messageContent.Pic            = ConvertImage.ToByte(UserInfo.AddedPicture);
                _messageContent.MessagePicture = @"C:\Users\X\Downloads\ChatData\ChatImage" + _messageContent.Id + ".jpg";

                _messageContent.PictureChanged = true;
            }


            var messageInBytes = ConverData.ToSend(_messageContent);

            try
            {
                TcpSocket.tcpSocket.BeginSend(messageInBytes, 0, messageInBytes.Length, SocketFlags.None, new AsyncCallback(SendCallback), TcpSocket.tcpSocket);
            }
            catch (Exception ex)
            {
                AlertMessages.Show(ex.Message);

                OnlineUsers.UserList.Clear();

                TcpSocket.tcpSocket.Shutdown(SocketShutdown.Both);
                TcpSocket.tcpSocket.Disconnect(true);

                _windowsViewModel.ChangeView(0);

                return;
            }

            SendText = "";

            UserInfo.AddedPicture = null;
            _messageContent.Pic   = null;
        }
Example #4
0
        public void Receiving(IWindowsViewModel windowsViewModel, ISignInViewModel loginViewModel, IMessageContent messageContent,
                              ITcpEndPoint tcpEndPoint, IJsonContainer container)
        {
            _tcpEndPoint      = tcpEndPoint;
            _windowsViewModel = windowsViewModel;

            FirstTime = true;

            IsConnected = true;

            var uiContext = SynchronizationContext.Current;

            Task.Run(() =>
            {
                ConnectingToServer();

                TcpSocket.tcpSocket.BeginSend(ConverData.ToSend(PersonList.GetPersonInfo()), 0, ConverData.ToSend(PersonList.GetPersonInfo()).Length, 0, new AsyncCallback(SendCallback), TcpSocket.tcpSocket);
                connectDone.WaitOne();

                while (IsConnected)
                {
                    _windowsViewModel.ChangeView(2);

                    var textFromServer = ReceivData(uiContext, container);

                    try
                    {
                        container = ConverData.ToReceiv <JsonContainer>(textFromServer.ToString());
                    }
                    catch (Exception ex)
                    {
                        Console.Write(ex.ToString());
                    }

                    if (FirstTime)
                    {
                        if (container.CurrentPersonId != null)
                        {
                            User.Id = container.CurrentPersonId.Id;
                        }

                        AddToOnlineUserList(uiContext, container);

                        GetAllMessages(messageContent, container, uiContext);


                        FirstTime = false;
                    }
                    else
                    {
                        // Detect if OnlineUsers changed
                        if (container?.Persons?.Count > OnlineUsers.UserList.Count || container?.Persons?.Count < OnlineUsers.UserList.Count)
                        {
                            ChangeOnlineUserList(uiContext, container, textFromServer.ToString());
                        }
                        else
                        {
                            AddNewMessage(uiContext, messageContent, textFromServer.ToString());
                        }
                    }
                }
            });
        }
Example #5
0
 private void ToSignIn()
 {
     _windowsViewModel.ChangeView(0);
 }
 private void ToLogin()
 {
     _windowsViewModel.ChangeView(1);
 }