예제 #1
0
        public void SentMessage(MessageEventArgsModel messageEventArgsModel)
        {
            string messageJson = JsonConvert.SerializeObject(messageEventArgsModel);

            _sWriter.WriteLine(messageJson);
            _sWriter.Flush();
        }
예제 #2
0
        private void btn_Sent_Click(object sender, RoutedEventArgs e)
        {
            var isAuthenticated = _clientTcp.IsAuthenticated();

            if (!isAuthenticated || string.IsNullOrWhiteSpace(textSent.Text))
            {
                return;
            }

            var selectedConversation = (ConversationModel)listBox.SelectedItem;

            if (selectedConversation != null)
            {
                var message = new MessageEventArgsModel
                {
                    Text            = textSent.Text,
                    MessageType     = MessageType.ToAnotherClient,
                    ConversationId  = selectedConversation.Id,
                    ClientId        = _currentСlient.Id,
                    PostingDateTime = DateTime.Now
                };
                var messageId = _common.SaveMessage(message);
                message.Id = messageId;
                _clientTcp.SentMessage(message);
                textSent.Text = string.Empty;
            }
        }
예제 #3
0
 private void SentStartMessage(object sender, MessageEventArgsModel e)
 {
     e.ClientId        = _currentСlient.Id;
     e.MessageType     = MessageType.Authenticate;
     e.PostingDateTime = DateTime.Now;
     _clientTcp.SentMessage(e);
 }
예제 #4
0
파일: Server.cs 프로젝트: DenGrom/tcp
        private static void SentToClient(StreamWriter sWriter, MessageEventArgsModel message)
        {
            string messageJson = JsonConvert.SerializeObject(message);

            sWriter.WriteLine(messageJson);
            sWriter.Flush();
        }
예제 #5
0
파일: Server.cs 프로젝트: DenGrom/tcp
 private static void SetClientId(ClientModel x, MessageEventArgsModel message)
 {
     if (x.Port == _startsPortNumber)
     {
         x.ClientId = message.ClientId;
     }
 }
예제 #6
0
 private void SetMessage(object sender, MessageEventArgsModel e)
 {
     this.Dispatcher.Invoke((Action)(() =>
     {
         Messages.Add(e);
         textBox.ItemsSource = Messages;
     }));
 }
예제 #7
0
파일: Common.cs 프로젝트: DenGrom/tcp
        public int SaveMessage(MessageEventArgsModel message)
        {
            var entityMessage = new Message {
                Client = new Client {
                    Id = message.ClientId
                },
                FromPort     = message.FromPort,
                Conversation = new Conversation {
                    Id = message.ConversationId
                },
                MessageType     = (int)message.MessageType,
                PostingDateTime = message.PostingDateTime,
                Text            = message.Text,
                ToPort          = message.ToPort
            };
            var messageId = _clientRepository.SaveMessage(entityMessage);

            return(messageId);
        }
예제 #8
0
        public void StartTcpClient(int portNumber)
        {
            _portNumber = portNumber;
            var ipPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), portNumber);

            _client = new TcpClient();
            _client.Connect(ipPoint);

            _sReader     = new StreamReader(_client.GetStream(), Encoding.ASCII);
            _sWriter     = new StreamWriter(_client.GetStream(), Encoding.ASCII);
            _isConnected = true;
            HandleCommunication();
            if (!_isAuthenticated)
            {
                var message = new MessageEventArgsModel {
                };
                //SentMessage(message);
                SentStartMessage(this, message);
            }
        }