Exemplo n.º 1
0
        /// <summary>
        /// Method which runs infinitely until an exit command is provided
        /// Takes user input for the messages to be sent in the chat
        /// </summary>
        static void Messaging()
        {
            Msg InitialMessage = new Msg("Joined", Msg.Code.NewUser, _screenName, null);

            _currentConsole.Display(UI.dCode.Message, InitialMessage, _listUsers);

            while (true)
            {
                string message = Console.ReadLine();

                if (message.Length > 0)
                {
                    try
                    {
                        Utility.Msg msg;
                        if (message.Equals("exit", StringComparison.OrdinalIgnoreCase))
                        {
                            _cache.RaiseCustomEvent(Utility.Msg.Code.UserLeft, _screenName);
                            return;
                        }
                        else
                        {
                            msg = new Utility.Msg(message, Utility.Msg.Code.NewUser, _screenName, null);
                            _cache.RaiseCustomEvent(Utility.Msg.Code.Text,
                                                    Utility.Helper.ToByteBuffer(msg));
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Cache custom event that is raised everytime a change occurs like
        /// new user joining
        /// user leaving
        /// message recieved
        /// </summary>
        /// <param name="opcode"></param>
        /// <param name="operand"></param>
        static void OnChatRoomEvent(object opcode, object operand)
        {
            Utility.Msg.Code code = (Utility.Msg.Code)opcode;
            Utility.Msg      msg;
            switch (code)
            {
            case Utility.Msg.Code.NewUser:
                string userName = operand as string;
                if (!_listUsers.Contains(userName))
                {
                    _listUsers.Add(userName);
                    msg = new Utility.Msg("Just joined the chat room", Utility.Msg.Code.NewUser, userName, null);
                    _currentConsole.Display(Utility.UI.dCode.Message, msg, _listUsers);
                }
                break;

            case Utility.Msg.Code.UserLeft:
                userName = operand as string;
                if (_listUsers.Contains(userName))     // if user exist in list
                {
                    _listUsers.Remove(userName);
                    msg = new Utility.Msg("Left the chat room", Utility.Msg.Code.NewUser, userName, null);
                    _currentConsole.Display(Utility.UI.dCode.Message, msg, _listUsers);
                }
                break;

            case Utility.Msg.Code.Text:
            {
                msg = Utility.Helper.FromByteBuffer(operand as byte[]) as Utility.Msg;
                if (msg == null)
                {
                    return;                      /// not a message maybe
                }
                if ((msg.To == null))
                {
                    _currentConsole.Display(Utility.UI.dCode.Message, msg, _listUsers);
                }
            }
            break;
            }
        }