public async Task ConnectAsync()
        {
            connection = new HubConnection(url);
            hubProxy   = connection.CreateHubProxy("ChatHub");
            hubProxy.On <User>("ParticipantLogin", (u) => ParticipantLoggedIn?.Invoke(u));
            hubProxy.On <string>("ParticipantLogout", (n) => ParticipantLoggedOut?.Invoke(n));
            hubProxy.On <string>("ParticipantDisconnection", (n) => ParticipantDisconnected?.Invoke(n));
            hubProxy.On <string>("ParticipantReconnection", (n) => ParticipantReconnected?.Invoke(n));
            hubProxy.On <string, string>("BroadcastTextMessage", (n, m) => NewTextMessage?.Invoke(n, m, MessageType.Broadcast));
            hubProxy.On <string, byte[]>("BroadcastPictureMessage", (n, m) => NewImageMessage?.Invoke(n, m, MessageType.Broadcast));
            hubProxy.On <string, string>("UnicastTextMessage", (n, m) => NewTextMessage?.Invoke(n, m, MessageType.Unicast));
            hubProxy.On <string, byte[]>("UnicastPictureMessage", (n, m) => NewImageMessage?.Invoke(n, m, MessageType.Unicast));
            hubProxy.On <User>("UnicastFriendshipRequest", (u) => NewFriendshipRequest?.Invoke(u, MessageType.Unicast));
            hubProxy.On <User>("AddFriend", (u) => NewFriendshipAdd?.Invoke(u, MessageType.Unicast));
            hubProxy.On <string>("ParticipantTyping", (p) => ParticipantTyping?.Invoke(p));

            connection.Reconnecting += Reconnecting;
            connection.Reconnected  += Reconnected;
            connection.Closed       += Disconnected;

            ServicePointManager.DefaultConnectionLimit = 10;
            await connection.Start();
        }
Esempio n. 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
                    });
                }
            }
        }