コード例 #1
0
ファイル: Connect.cs プロジェクト: ArtursSilins/ChatServer
 private void SendUsersOnlineToClients()
 {
     foreach (var item in UsersOnline.Persons)
     {
         item.Connection.Send(ConverData.ToSend(_jsonContainer));
     }
 }
コード例 #2
0
        public void Disconnect()
        {
            _disconnectContent = Factory.CreateDisconnectContent();

            _disconnectContent.Id = User.Id;

            TcpSocket.tcpSocket.Send(ConverData.ToSend(_disconnectContent));

            TcpSocket.tcpSocket.Shutdown(SocketShutdown.Both);
            TcpSocket.tcpSocket.BeginDisconnect(true, new AsyncCallback(DisconnectCallback), TcpSocket.tcpSocket);
        }
コード例 #3
0
        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;
        }
コード例 #4
0
 public static void SendAsync(object data)
 {
     TcpSocket.tcpSocket.BeginSend(ConverData.ToSend(data), 0, ConverData.ToSend(data).Length, 0, new AsyncCallback(SendCallback), TcpSocket.tcpSocket);
     connectDone.WaitOne();
 }
コード例 #5
0
ファイル: Connect.cs プロジェクト: ArtursSilins/ChatServer
        private void Chat(string textFromClient, Socket socket)
        {
            if (FirstTime)
            {
                FirstTime = false;

                _person = ConverData.ToReceiv <Person>(textFromClient);

                _person.PersonId   = _PersonId;
                _person.Connection = socket;

                UsersOnline.Persons.Add(_person);

                ShowUsersOnline(_uIViewModel);

                AddPersonsToJsonContainer(_jsonContainer);

                foreach (var user in UsersOnline.Persons)
                {
                    if (user.PersonId == _PersonId)
                    {
                        _jsonContainer.CurrentPersonId.Id = _PersonId;

                        AddMessagesToJsonContainer(_jsonContainer);

                        user.Connection.Send(ConverData.ToSend(_jsonContainer));
                    }
                    else
                    {
                        _jsonContainer.Messages.Clear();
                        user.Connection.Send(ConverData.ToSend(_jsonContainer));
                    }
                }
            }
            else
            {
                DisconnectContent disconnectContent = new DisconnectContent();

                disconnectContent = ConverData.ToReceiv <DisconnectContent>(textFromClient);

                if (disconnectContent.ExitMessage != "€noc§dne§€")
                {
                    _messageContent = ConverData.ToReceiv <MessageContent>(textFromClient);

                    string tempName = _messageContent.Name;

                    AllMessages.Messages.Add(_messageContent.NewInstance(_messageContent));

                    AddToMessagesOnServer();

                    foreach (var item in UsersOnline.Persons)
                    {
                        if (item.PersonId == _messageContent.Id)
                        {
                            _messageContent.MessageAlignment         = "Right";
                            _messageContent.MessageColour            = SenderReceiwer.SendBubbleColor;
                            _messageContent.MessagePictureVisibility = "Hidden";
                            _messageContent.Name = "";
                        }
                        else
                        {
                            _messageContent.MessageAlignment         = "Left";
                            _messageContent.MessageColour            = SenderReceiwer.ReceiveBubleColor;
                            _messageContent.MessagePictureVisibility = "Visible";
                            _messageContent.Name = tempName;
                        }

                        item.Connection.Send(ConverData.ToSend(_messageContent));
                    }
                }
                else
                {
                    RemoveDisconnectedUser(disconnectContent);

                    SendUsersOnlineToClients();

                    ShowUsersOnline(_uIViewModel);
                }
            }
        }