Пример #1
0
        /// <summary>
        /// Called when data is received from the client.
        /// </summary>
        /// <param name="data">The data from the client.</param>
        private void OnDataReceived(string data)
        {
            // Only accept GET requests
            if (!data.StartsWith("GET"))
            {
                SendResponse(Resources.html_path_options);
                _state.Disconnect();
                return;
            }

            // Get path accessed
            var matches = PathMatchRegex.Matches(data);

            if (matches.Count == 0 || !matches[0].Groups[1].Success)
            {
                // A path could not be found in the request.
                SendResponse(Resources.html_path_options);
                _state.Disconnect();
                return;
            }
            var path = matches[0].Groups[1].Value;

            // Get key and value if applicable.
            var key   = matches[0].Groups[3].Success ? matches[0].Groups[3].Value : null;
            var value = matches[0].Groups[4].Success ? matches[0].Groups[4].Value : null;

            // Serve pages based on path.
            switch (path)
            {
            case "/scores":
                var scoresTable = _scoreboardServerController.GenerateScoresTable();
                SendResponse(Resources.html_scores.Replace("TABLE", scoresTable));
                break;

            case "/game":
                if (key == "id" && int.TryParse(value, out var id))
                {
                    var gameTable = _scoreboardServerController.GenerateGameTable(id);
                    SendResponse(Resources.html_game.Replace("TABLE", gameTable));
                }
                else
                {
                    SendResponse(Resources.html_path_options);
                }
                break;

            case "/games":
                if (key == "player")
                {
                    var gamesTable = _scoreboardServerController.GenerateGamesTable(value);
                    SendResponse(Resources.html_games.Replace("TABLE", gamesTable));
                }
                else
                {
                    SendResponse(Resources.html_path_options);
                }
                break;

            default:
                SendResponse(Resources.html_path_options);
                break;
            }

            _state.Disconnect();
        }