Exemplo n.º 1
0
 public void ConnectToServer()
 {
     ReceivedMessages.TryAdd("", "Please input your desired username...");
     Task connectingTask = Task.Run(() =>
     {
         int i = 0;
         if (_connection.EstablishConnection().Result || i < 100)
         {
             i = 0;
             while (_inputMessage == null || i < 1000)
             {
                 i++;
             }
             if (i < 90)
             {
                 _connection.Disconnect();
                 ReceivedMessages.TryAdd("", "Connection timeout. Please try re-connecting.");
                 OnPropertyChanged(nameof(ReceivedMessages));
                 return;
             }
             _userName    = InputMessage;
             InputMessage = "";
             if (_connection.RegisterUsername(_userName).Result)
             {
                 getMessagesTask.Start();
             }
         }
         else
         {
             _connection.Disconnect();
             ReceivedMessages.TryAdd("", "Connection Failed.");
             OnPropertyChanged(nameof(ReceivedMessages));
         }
     });
 }
Exemplo n.º 2
0
        /// <summary>
        /// Handles the event raised after a mod message is received
        /// </summary>
        /// <param name="sender">The sender of the ModMessageReceived event</param>
        /// <param name="args">Event arguments for the ModMessageReceived event</param>
        public void OnModMessageReceived(object sender, ModMessageReceivedEventArgs args)
        {
            Message message = args.ReadAs <Message>();

            ReceivedMessages.TryAdd(args.Type, new List <Message>());
            ReceivedMessages[args.Type].Add(message);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks for new messages from a particular farmer that haven't been handled yet
        /// </summary>
        /// <param name="address">Destination address to check for message</param>
        /// <param name="sender">ID of Farmer that sent a message</param>
        /// <returns></returns>
        public IEnumerable <Message> RetrieveMessages(string address, long sender = -1)
        {
            ReceivedMessages.TryAdd(address, new List <Message>());

            List <Message> messages = new List <Message>(ReceivedMessages[address]);

            foreach (Message message in messages)
            {
                if (sender == -1 || sender == message.Sender)
                {
                    Network.Instance.ReceivedMessages[address].Remove(message);
                    yield return(message);
                }
            }
        }
Exemplo n.º 4
0
 public ChatViewModel()
 {
     _connection         = new ConnectionHandler();
     _receivedMessages   = new ConcurrentDictionary <string, string>();
     _connectCommand     = new RelayCommand(ConnectToServer);
     _disconnectCommand  = new RelayCommand(_connection.Disconnect);
     _sendMessageCommand = new RelayCommand(SendMessage);
     getMessagesTask     = new Task(() =>
     {
         while (true)
         {
             string receivedMessage = _connection.ListenForMessage();
             string[] splitMsg      = receivedMessage.Split('~');
             ReceivedMessages.TryAdd(splitMsg[0], splitMsg[1]);
             OnPropertyChanged(nameof(ReceivedMessages));
         }
     });
 }