How Get TPS from Server?

Discussion in 'Programming' started by Kela, May 1, 2017.

  1. How get TPS from server?? PHP
     
  2. A query will not return the TPS and Minecraft does not support RCON so you'd have to make a plugin on the server to respond to a request and return the TPS, or alternatively continuously store the TPS in a database where the PHP script can access it.

    If you are using some kind of daemon or panel like Multicraft that would help. You can just dispatch a console command and get the return I would think.
     
  3. Uhh.. Minecraft does support rcon.
    http://wiki.vg/RCON
     
    • Agree Agree x 1
  4. Do not know what plugin is there for storage of TPS in MYSQL??
     
  5. Look at lagmonitor.
     
  6. So that? Can not I get it without the plugin?
     
  7. How did you plan on getting the TPS with a plugin? If you want to do it using a plugin, your plugin would have to open a port and export a JSON file for example.
     
  8. I would like to. I'm not in the class. Although somehow get TPS. With the help of the plugin, at least
     
  9. hey, somebody. Can help?
     
  10. I believe it's built into the API.

    MinecraftServer.getServer().recentTps
     
  11. I would write a plugin to either keep updating a local file or write a value to a database and read the file or database value from php and use ajax to update it every so often. If you wanted to have it be real time you could open a websocket and use javascript in the browser to connect and update it.

    Here is a websocket in a plugin for updating the tps through ajax:
    Code (Text):
    import java.io.BufferedWriter;
    import java.io.OutputStreamWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;

    public class TPSServer extends JavaPlugin {

        ServerSocket server;
        int tps = 0;
        long sec;
        long currentSec;
        int ticks;
        int port;

        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            port = getConfig().getInt("port");
            getLogger().info("Starting Web Server on Port " + port);
            try {
                server = new ServerSocket(port);
                getLogger().info("Web Server Started!");
            } catch (Exception e) {
                e.printStackTrace();
            }

            new Thread() {
                public void run() {
                    try {
                        listen();
                    } catch (Exception e) { }
                }
            }.start();

            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {

                public void run() {
                    sec = (System.currentTimeMillis() / 1000);
                    if (currentSec == sec) {
                        ticks++;
                    } else {
                        currentSec = sec;
                        tps = (tps == 0 ? ticks : ((tps + ticks) / 2));
                        ticks = 0;
                    }
                }
            }, 0, 1);
        }

        public void onDisable() {
            try {
                server.close();
            } catch (Exception e) { }
            Bukkit.getScheduler().cancelTasks(this);
            getLogger().info("Web Server Stopped!");
        }

        public void listen() throws Exception {
            while (true) {
                Socket client = server.accept();
                new Thread() {
                    public void run() {
                        try {
                            String s = tps + "";
                            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                            SimpleDateFormat sdfDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
                            Date now = new Date();
                            out.write("HTTP/1.0 200 OK\r\n");
                            out.write("Date: " + sdfDate.format(now) + "\r\n");
                            out.write("Server: TPSServer/1.0.0\r\n");
                            out.write("Content-Type: text/html\r\n");
                            out.write("Access-Control-Allow-Origin: *\r\n");
                            out.write("Content-Length: " + s.length() + "\r\n");
                            out.write("Expires: Thu, 01 Jan 1970 00:00:00 GMT\r\n");
                            out.write("Last-modified: " + sdfDate.format(now) + "\r\n");
                            out.write("\r\n");
                            out.write(s);
                            out.flush();
                            out.close();
                        } catch (Exception e) { }
                    }
                }.start();
            }
        }

    }
    if you visit the server's address on port 8080 or whatever port you put in the config it should work beautifully.

    here is the code for the ajax portion
    HTML:
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script>
    setInterval(function(){
      $.ajax({
        url:"http://localhost:82",
        success:function(tps){
          $('body').html("TPS = " + tps);
          console.log(tps);
        }
      });
    }, 1000);
    </script>
     
    also don't judge the code... it was thrown together in 10 minutes just to show as an example. adapt as you please
     
    #13 proferabg, May 5, 2017
    Last edited: May 5, 2017
    • Like Like x 1