Ejemplo n.º 1
0
        private void ContextProcessing(HttpListenerContext context)
        {
            var    requestPath = context.Request.Url.LocalPath;
            string error       = null;

            switch (requestPath)
            {
            case IndexPath:
                IndexHandler(context);
                return;

            case LoginPath:
                var clientName      = LoginHandler(context, out var SessionId);
                var newClientObject = ChatHandlers.SignInChat(clientName, out error);
                if (newClientObject != null)
                {
                    UserSessions.Add(SessionId, newClientObject);
                }
                break;

            case LogoutPath:
                var clientObject = LogoutHandler(context);
                ChatHandlers.LogOutChat(clientObject, out error);
                break;

            case PostMessagePath:
                clientObject = PostMessageHandler(context, out var userMessage, out error);
                if (userMessage != null)
                {
                    ChatHandlers.AddMessageChat(clientObject, userMessage, out error);
                }
                break;

            case DeleteMessagePath:
                var messageId = DeleteMessageHandler(context);
                ChatHandlers.DeleteMessageChat(messageId, out error);
                break;
            }

            if (error == null)
            {
                Redirect(context, Host + IndexPath);
            }
            else
            {
                ErrorHandler(context, error);
            }
        }
Ejemplo n.º 2
0
        private void MessageProcessing(string message)
        {
            string error = null;

            switch (ChatCmd.GetCommandType(message))
            {
            case CommandAndEventType.CommandTypes.START_CHAT:
                IsWorking = true;
                Render(ChatInfo);
                break;

            case CommandAndEventType.CommandTypes.SIGNIN:
                var clientName      = GetClientName(message);
                var newClientObject = ChatHandlers.SignInChat(clientName, out error);
                if (newClientObject != null)
                {
                    OnlineClients.Add(clientName, newClientObject);
                }
                break;

            case CommandAndEventType.CommandTypes.LOGOUT:
                var clientObject = GetUserFromMessage(message);
                ChatHandlers.LogOutChat(clientObject, out error);
                break;

            case CommandAndEventType.CommandTypes.ADD_MESSAGE:
                clientObject = GetUserFromMessage(message, out var userMessage);
                ChatHandlers.AddMessageChat(clientObject, userMessage, out error);
                break;

            case CommandAndEventType.CommandTypes.DELETE_MESSAGE:
                var messageId = GetMessageId(message);
                ChatHandlers.DeleteMessageChat(messageId, out error);
                break;

            case CommandAndEventType.CommandTypes.BOT:
                clientObject = GetUserFromMessage(message);
                GetBotInfo(message, out var botName, out var botCommand, out var commandArgument);
                ChatHandlers.CallBot(clientObject, botName, botCommand, commandArgument, out error);
                break;

            case CommandAndEventType.CommandTypes.STOP_CHAT:
                IsWorking = false;
                break;

            case CommandAndEventType.CommandTypes.HELP:
                Render(ChatInfoBase);
                break;

            case CommandAndEventType.CommandTypes.ERROR:
                error = "Введена неверная команда";
                break;
            }
            new Thread((object botThread) =>
            {
                if (error != null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Render(error);
                    Console.ResetColor();
                }
                else
                {
                    while (ChatHandlers.BotThread != null && ChatHandlers.BotThread.IsAlive)
                    {
                    }
                    foreach (var messageText in ChatHandlers.GetMessages())
                    {
                        Render(string.Format(MessageFormat, messageText.UserName, messageText.TextOfMessage, messageText.TimeOfMessage));
                    }
                }
            }
                       ).Start();
        }