Пример #1
0
        private void CommandSwitchHandler(string commandJsonString)
        {
            var commandJson = JsonConvert.DeserializeObject <CommandMessageContainer>(commandJsonString);

            if (commandJson.Command == CommandType.Start && !usersService.IsAuthorised(UserObject.AccessToken))
            {
                commandHandler.Start(this, commandJson.Args["AccessToken"].ToString());
            }

            else if (usersService.IsAuthorised(UserObject.AccessToken))
            {
                switch (commandJson.Command)
                {
                case CommandType.SetUsername:
                    commandHandler.SetUsername(this, commandJson.Args["Name"].ToString());
                    break;

                case CommandType.Here:
                    commandHandler.Here(this);
                    break;

                case CommandType.Ping:                         // it's possible to encapsulate this into commandHandler.Pong
                    try
                    {
                        sender.Pong(this, commandJson.Args["ID"].ToObject <int>());
                    }
                    catch
                    {
                        sender.ServerNotification(this, "Incorrect ID for command /Ping");
                    }
                    break;

                case CommandType.Pong:
                    CheckPong(commandJson.Args["ID"].ToObject <int>());
                    break;

                case CommandType.SendFile:
                    sender.BroadcastFile(this, commandJson.Args);
                    break;

                default:
                    sender.ServerNotification(this, "Incorrect command.");
                    break;
                }
            }
            else
            {
                sender.ServerNotification(this, "You are not authorized.");
            }
        }