Ejemplo n.º 1
0
        private void InitializeConnection(string url, Cookie authCookie)
        {
            _connection = new HubConnection(url);
            _connection.CookieContainer = new CookieContainer();
            _connection.CookieContainer.Add(authCookie);

            // Get a reference to the chat proxy
            _chat = new Chat(_connection);

            // Start the connection
            _connection.Start().ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    // Show a connection error here
                }
                else
                {
                    // Get a list of users and add it to the view model
                    _chat.GetUsers().ContinueWith(getUserTask =>
                    {
                        if (getUserTask.IsFaulted)
                        {
                            // Show a connection error here
                        }
                        else
                        {
                            // Exclude current user from Contact list
                            User.Contacts = new ObservableCollection<User>(
                                getUserTask.Result.Where(u => !u.Name.Equals(User.Name, StringComparison.OrdinalIgnoreCase))
                            );
                        }
                    });
                }
            });

            // Fire events on the ui thread
            _chat.UserOffline += user => _syncContext.Post(_ => OnUserOffline(user), null);
            _chat.UserOnline += user => _syncContext.Post(_ => OnUserOnline(user), null);
            _chat.Message += message => _syncContext.Post(_ => OnMessage(message), null);
        }
Ejemplo n.º 2
0
        private void InitializeConnection(string url, Cookie authCookie)
        {
            _connection = new HubConnection(url) { CookieContainer = new CookieContainer() };
            _connection.CookieContainer.Add(authCookie);

            // Get a reference to the chat proxy
            _chat = new Chat(_connection);

            // Start the connection
            _connection.Start().ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    // Show a connection error here
                }
                else
                {
                    _chat.GetUser(Name).ContinueWith(getUserTask =>
                    {
                        if (getUserTask.IsFaulted)
                        { }
                        else
                        {
                            if (getUserTask.Result != null)
                            {
                                Me = new ContactViewModel(getUserTask.Result);
                                Me.OpenChatEvent += OnOpenChat;

                                _chat.GetConversations(Me.User).ContinueWith(getConversationTask =>
                                {
                                    if (getConversationTask.IsFaulted)
                                    {
                                        Debug.WriteLine("GetConversations Failed: " + getConversationTask.Status);
                                    }
                                    else
                                    {
                                        var conversations =  from Message message in getConversationTask.Result
                                                             select new ContactViewModel(message.Contact, message, Me);
                                        Conversations = new ObservableCollection<ContactViewModel>(conversations);
                                        Debug.WriteLine("Conversations Count: " + conversations.Count());
                                    }
                                });

                                // Get a list of users and add it to the view model
                                _chat.GetUsers().ContinueWith(getUsersTask =>
                                {
                                    if (getUsersTask.IsFaulted)
                                    {
                                        // Show a connection error here
                                    }
                                    else
                                    {
                                        // Exclude current user from Contact list
                                        var contacts = from User user in getUsersTask.Result
                                                       where !user.Name.Equals(Me.User.Name, StringComparison.OrdinalIgnoreCase)
                                                       select new ContactViewModel(user, Me);

                                        Contacts = new ObservableCollection<ContactViewModel>(contacts);
                                        Debug.WriteLine("Contacts Count: " + contacts.Count());
                                    }
                                });
                            }
                        }
                    });
                }
            });

            // Fire events on the ui thread
            _chat.UserOffline += user => _syncContext.Post(_ => OnUserStatusChange(user), null);
            _chat.UserOnline += user => _syncContext.Post(_ => OnUserStatusChange(user), null);
            _chat.Message += message => _syncContext.Post(_ => OnMessage(message), null);
        }