[HELP] How do I display player count on website?

I hope this is the right section…

I’m wondering how to get the player count of a server and display it on my website. I have searched the forums, but any links I’ve come across are down. Looking to display it like - [3/32] as an example. Don’t need the player names, just the count.

Is this possible?

Thanks in advance!

1 Like

Request the /dynamic.json endpoint on your server port, it contains ‘clients’ and ‘sv_maxclients’

1 Like

Thank you :slight_smile:

So I just do serverip:serverport/dynamic.json?

Yes it will be returned in JSON

I apologize, I’m not totally sure how to properly set it code up to fetch those details. Any tips? I appreciate your help!

1 Like

I don’t know in which language you’re working in, but in PHP something like this would work

$response = file_get_contents("http://localhost:30120/dynamic.json");
$json = json_decode($response);

echo "{$json->clients}/{$json->sv_maxclients}";
3 Likes

Your code helped me, ALOT! Thank you thank you thank you!

This is what the final results are and what is working :slight_smile:

$file = file_get_contents('http://ipaddress:port/dynamic.json');

$decode = json_decode($file, false);
$clients = $decode->clients;
$svmaxclients = $decode->sv_maxclients;

echo '<span class="label">' . $clients . '/' . $svmaxclients . '</span>';

I appreciate your help!

Can i jump in here and check if someone know how to do it with node.js

Something like this would work in node.js using the node-fetch module

const fetch = require("node-fetch")

fetch("http://ip:port/dynamic.json")
    .then(x => x.json())
    .then(json => console.log(`${json.clients}/${json.sv_maxclients}`))
    .catch(console.error)

Thanks mate

hey any chance someone know how to do this with visual studio code ?

What? VSCode is an editor, not a programming language. What language are you actually trying to do it in?

im using cs script

‘cs script’ assuming C#

using (HttpClient client = new HttpClient())
{
    var response = await client.GetStringAsync("http://ip:port/dynamic.json");
    var info = JsonConvert.DeserializeObject<DynamicResponse>(response);

    Console.WriteLine($"{info.Clients}/{info.MaxClients} online");
}

// 2021 edit - can use System.Text.Json, also should probably reuse HttpClient
// also in C# 9.0 or so idk you can use `using var client = new HttpClient()`
class DynamicResponse
{
    [JsonProperty("hostname")]
    public string Hostname { get; set; }

    [JsonProperty("gametype")]
    public string GameType { get; set; }

    [JsonProperty("mapname")]
    public string MapName { get; set; }

    [JsonProperty("clients")]
    public int Clients { get; set; }

    [JsonProperty("iv")]
    public long Version { get; set; }  // not sure if this should be a long, and not just a string

    [JsonProperty("sv_maxclients")]
    public int MaxClients { get; set; }
}

Using Newtonsoft.Json, System.Text.Json also works but that doesn’t support automatic converting to type without writing big jsonconverter blegh (edit it does now in .NET 5)

(you can also deserialize to dynamic or JObject but MEH)

i have a issue with this part of the code

            var response = await client.GetStringAsync("http://ip:port/dynamic.json");
            var info = JsonConvert.DeserializeObject<DynamicResponse>(response);

            Console.WriteLine($"{info.Clients}/{info.MaxClients} online");
        }

my plan is to get it running on a launcher for a static player count that updates

I suggest you follow a C# course

I search in java scrip does it’s possible ?

If you scroll up you see code for JS, C# and PHP

it is possible to have in HTLM?