Exemplo n.º 1
0
        /// <summary>
        /// Gets the current callback channel and adds it to the collection of all channels.
        /// </summary>
        /// <returns></returns>
        public ChannelWrapper RegisterClient(Message message)
        {
            var manager = new NewsfeedManager();

            string username;
            if (manager.TryGetCurrentUsername(message, out username))
            {
                ChannelWrapper wrapper;

                //If the user is logged and resfresh the page, a new channel will be opened and the old should be removed
                if (this.clients.TryGetValue(username, out wrapper))
                {
                    this.clients.Remove(username);
                }

                var client = OperationContext.Current.GetCallbackChannel<INewsfeedServiceCallback>();

                wrapper = new ChannelWrapper(username, client);
                wrapper.Closed += Connection_Closed;
                wrapper.Faulted += Connection_Closed;

                var usersRepository = new Domain.UserRepository();
                wrapper.User = usersRepository.Get(username);

                this.clients.Add(username, wrapper);

                return wrapper;
            }

            return null;
        }
Exemplo n.º 2
0
        public void Recieve(Message message)
        {
            var manager = new NewsfeedManager();

            string username;
            if (!manager.TryGetCurrentUsername(message, out username) || !manager.ValidateSameOrigin(message))
            {
                //If the sender is not logged user or the message comes from different domain do nothing
                return;
            }

            if (!message.IsEmpty) //New message is recieved
            {
                var content = manager.GetMessage(message);

                var action = (ServiceAction)Enum.Parse(typeof(ServiceAction), content.Action);

                switch (action)
                {
                    case ServiceAction.ShowMore:
                        var messagesRepo = new Domain.MessageRepository();
                        var messages = messagesRepo.GetLatestMessages(content.DisplayedMessages, 20, this.currentClient.User.BlockedUsers);
                        this.SendOlderMessagesToClient(messages, manager);
                        break;
                    case ServiceAction.NewMessage:
                        manager.SaveMessage(content, this.currentClient.User);
                        this.BroadcastMessage(content);
                        break;
                    case ServiceAction.LikeMessage:
                        manager.LikeMessage(content);
                        this.BroadcastMessage(content);
                        this.SendLikeNotification(content, username);
                        break;
                    case ServiceAction.BlockUser:
                        manager.BlockUser(this.currentClient.User, content);
                        break;
                    default:
                        break;
                }
            }
            else //New connection has been created and this is her opening message
            {
                ClientsManager.Instance.ClearFailed();

                //Associate the opened channel with the logged user
                this.currentClient = ClientsManager.Instance.RegisterClient(message);

                var messagesRepo = new Domain.MessageRepository();
                var messages = messagesRepo.GetLatestMessages(0, 20, this.currentClient.User.BlockedUsers);

                this.SendOlderMessagesToClient(messages, manager);

                var hello = new Model.Message()
                {
                    Action = ServiceAction.Notification.ToString(),
                    Text = "Welcome to the newsfeed!",
                    SentDate = DateTime.Now
                };
                this.currentClient.Callback.Send(manager.CreateMessage(hello));
            }
        }