Search code examples
phphtmlmysqlheartbeat

Getting a HeartBeat from a C# Application and Posting it To Website


I've got a Minecraft Software written in C# that I want to send a heartbeat to my site. I've got the way to send the beat already written.

 if (Server.Uri == null) return;
        string uri = "http://GemsCraft.comli.com/Heartbeat.php";
        // create a request
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";

            // turn request string into a byte stream
            byte[] postBytes = Encoding.ASCII.GetBytes(string.Format("ServerName={0}&Url={1}&Players={2}&MaxPlayers={3}&Uptime={4}",
                             Uri.EscapeDataString(ConfigKey.ServerName.GetString()),
                             Server.Uri,
                             Server.Players.Length,
                             ConfigKey.MaxPlayers.GetInt(),
                             DateTime.UtcNow.Subtract(Server.StartTime).TotalMinutes));

            request.ContentType = "application/x-www-form-urlencoded";
            request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
            request.ContentLength = postBytes.Length;
            request.Timeout = 5000;
            Stream requestStream = request.GetRequestStream();

            // send it
            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Flush();
            requestStream.Close();
            /* try
             {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Logger.LogToConsole(new StreamReader(response.GetResponseStream()).ReadToEnd());
                Logger.LogToConsole(response.StatusCode + "\n");
             }
             catch (Exception ex)
             {
                 Logger.LogToConsole("" + ex);
             }*/
        }

Now, I want to be able to retrieve the heartbeat in PHP, upload it to the SQL database, and then display each user's server in a table that will be displayed on the webpage

How do I do this?


Solution

  • portforwardpodcast's answer isn't very well-suited for your purposes, here's a process for you to ponder

    Server accesses the following page: heartbeat.php?port=25565&maxplayers=25&players=2&name=Cheese_Pizza_Palace

    Your PHP script will then do the following...

    • Go through each value, making sure they're all the types you want them to be (integers/strings)
    • Connect to the database
    • Update the server in the database if it already exists, create it if it doesn't
    • Return some value so the server knows that it completed successfully.

    And to display the servers

    • Fetch all 'active' servers
    • Loop through them and display each one.

    Things you'll need to figure out:

    • How to determine uptime
    • How to determine "active" servers
    • How to update/create MySQL entries
    • How to (properly) connect to a database. I would suggest using PDO since you're using PHP. It's a bit difficult to learn, but it's much more secure than writing the queries directly.
    • How to loop through all the GET variables.

    Good hunting!