Beispiel #1
0
        /// <summary>
        /// Sends message to server (and re-send if needs).
        /// </summary>
        /// <param name="message">message to send.</param>
        private void SendMessage(Message message)
        {
            if (!isLoggedIn)
            {
                return;
            }

            try
            {
                client.SendMessage(message);
            }
            catch (ConnectionInterruptedException)
            {
                try
                {
                    // try again
                    client.SendMessage(message);
                }
                catch (ConnectionInterruptedException)
                {
                    // fail two times -> something wrong with connection
                    Logout();
                    ConnectionBreaks?.Invoke();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Listen for messages from server.
        /// </summary>
        public async void Listen()
        {
            if (!isLoggedIn)
            {
                return;
            }

            // message from server
            Message message;

            while (true)
            {
                try
                {
                    // wait for message
                    message = await client.ReceiveMessageAsync();
                }
                catch (ConnectionInterruptedException)
                {
                    if (isLoggedIn)
                    {
                        ConnectionBreaks?.Invoke();
                    }

                    return;
                }

                // handle message
                if (message is AllUsersMessage)
                {
                    OnlineUsersList  = new List <string>(((AllUsersMessage)message).OnlineUsers);
                    OfflineUsersList = new List <string>(((AllUsersMessage)message).OfflineUsers);
                }
                else if (message is FriendsMessage)
                {
                    OnlineFriendsList  = new List <string>(((FriendsMessage)message).OnlineFriends);
                    OfflineFriendsList = new List <string>(((FriendsMessage)message).OfflineFriends);
                }
                else if (message is IncomeFriendshipRequestsMessage)
                {
                    IncomeRequestsList = new List <string>(((IncomeFriendshipRequestsMessage)message).Logins);
                }
                else if (message is OutcomeFriendshipRequestsMessage)
                {
                    OutcomeRequestsList = new List <string>(((OutcomeFriendshipRequestsMessage)message).Logins);
                }
                else if (message is UserActionMessage)
                {
                    UserActionMessage msg = (UserActionMessage)message;

                    switch (msg.Action)
                    {
                    case UserActions.GoOnline:
                        FriendGoOnline?.Invoke(msg.UserLogin);
                        break;

                    case UserActions.GoOffline:
                        FriendGoOffline?.Invoke(msg.UserLogin);
                        break;

                    case UserActions.AcceptFriendship:
                        FriendshipAccepted?.Invoke(msg.UserLogin, msg.Time);
                        OutcomeRequestsList?.Remove(msg.UserLogin);
                        if (OnlineFriendsList != null && !OnlineFriendsList.Contains(msg.UserLogin))
                        {
                            OnlineFriendsList.Add(msg.UserLogin);
                        }
                        break;

                    case UserActions.RejectFriendship:
                        FriendshipRejected?.Invoke(msg.UserLogin, msg.Time);
                        OutcomeRequestsList?.Remove(msg.UserLogin);
                        break;

                    case UserActions.SendFriendshipRequest:
                        NewFriendshipRequest?.Invoke(msg.UserLogin, msg.Time);
                        if (IncomeRequestsList != null && !IncomeRequestsList.Contains(msg.UserLogin))
                        {
                            IncomeRequestsList.Add(msg.UserLogin);
                        }
                        break;

                    case UserActions.CancelFriendshipRequest:
                        FriendshipRequestCancelled?.Invoke(msg.UserLogin, msg.Time);
                        IncomeRequestsList?.Remove(msg.UserLogin);
                        break;

                    case UserActions.RemoveFromFriends:
                        RemovedFromeFriends?.Invoke(msg.UserLogin, msg.Time);
                        OnlineFriendsList?.Remove(msg.UserLogin);
                        OfflineFriendsList?.Remove(msg.UserLogin);
                        break;

                    case UserActions.MessageSended:
                        MessageSended?.Invoke(msg.UserLogin, msg.Time);
                        break;

                    default:
                        break;
                    }
                }
                else if (message is NewReplyMessage)
                {
                    Conversations.AddReply(
                        ((NewReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((NewReplyMessage)message).Author,
                        Time   = ((NewReplyMessage)message).Time,
                        Text   = ((NewReplyMessage)message).Text
                    }
                        );
                    NewReplyComes?.Invoke(
                        ((NewReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((NewReplyMessage)message).Author,
                        Time   = ((NewReplyMessage)message).Time,
                        Text   = ((NewReplyMessage)message).Text
                    });
                }
                else if (message is OldReplyMessage)
                {
                    Conversations.InsertReplyToTop(
                        ((OldReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((OldReplyMessage)message).Author,
                        Time   = ((OldReplyMessage)message).Time,
                        Text   = ((OldReplyMessage)message).Text
                    }
                        );
                    OldReplyComes?.Invoke(
                        ((OldReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((OldReplyMessage)message).Author,
                        Time   = ((OldReplyMessage)message).Time,
                        Text   = ((OldReplyMessage)message).Text
                    });
                }
            }
        }