Exemplo n.º 1
0
        public void ProcessMessage()
        {
            string userName;

            if (handShaker.Shake(out userName))
            {
                Task.Factory.StartNew(() =>
                {
                    while (socketWrapper.Connected)
                    {
                        socketWrapper.Write(FrameHelper.Ping());
                        Thread.Sleep(25000);
                    }
                });
                Notify(MessageFactory.CreateHistory());
                Notify(MessageFactory.CreateMessage(string.Format("{0} has connected", userName)));

                Storage.UsersStorage.Add(userName);
                channelsNotifier.NotifyAll(MessageFactory.CreateUsersList());

                while (true)
                {
                    var message = socketWrapper.Read();
                    if (RequestParser.CloseChannel(message))
                    {
                        socketWrapper.Close();
                        Storage.UsersStorage.Remove(userName);
                        channelsNotifier.RemoveChannel(this);
                        channelsNotifier.NotifyAll(MessageFactory.CreateUsersList());
                        channelsNotifier.NotifyAll(MessageFactory.CreateMessage(string.Format("{0} has disconnected", userName)));
                    }
                    else
                    {
                        if (!RequestParser.IsPongMessage(message))
                        {
                            var messageStr = FrameHelper.DecodeMessage(message);
                            Storage.MessagesStorage.Add(new KeyValuePair <string, string>(userName, messageStr));
                            channelsNotifier.Notify(this, MessageFactory.CreateMessage(string.Format("{0}:{1}", userName, messageStr)));
                        }
                    }
                }
            }
        }