PHP code example of rollylni / rcon-protocol

1. Go to this page and download the library: Download rollylni/rcon-protocol library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rollylni / rcon-protocol example snippets



use RconProtocol\RconServer;
use RconProtocol\ServerClient;
use RconProtocol\Packet;

$port = 27015;
$host = "0.0.0.0";

$server = new RconServer($port, $host);

# set ur password
$server->setPassword("Your Password");
# or generate
$len = 8;
$server->generatePassword($len);

$server->setMaxClients(10);
# client login timeout
$server->setTimeout(10);

# Events
$handler = $server->getHandler();
$handler->add("onTimeout", function(ServerClient $c) {
   echo $c->getPeerName() . ": timeout!\n";
});

$handler->add("onConnection", function(ServerClient $c) {
   echo $c->getPeerName() . ": connection...\n";
});

$handler->add("onDisconnection", function(ServerClient $c) {
   echo $c->getPeerName() . ": disconnection...\n";
});

$handler->add("onReceive", function(ServerClient $c, Packet $p) {
   echo $c->getPeerName() . ": ". $p->getBody() ."\n";
});

$handler->add("onAuthorized", function(ServerClient $c) {
   echo $c->getPeerName() . ": authorized!\n";
});

$handler->add("onFailed", function(ServerClient $c, string $input) {
   echo $c->getPeerName() . ": '$input' wrong password!\n";
});

$handler->add("onCommand", function(ServerClient $c, string $cmd) {
   //exec command
   echo $c->getPeerName() . ": $cmd\n";
});

// Starting RCON server
$server->start();
// Stopping
$server->stop();

use RconProtocol\RconClient;

$serverHost = "0.0.0.0";
$serverPort = 27015;
$serverPassword = "";
$execCommand = "cmd";
$timeout = 10;

$client = new RconClient($serverHost, $serverPort, $timeout);
$client->connect();
if ($client->authorize($serverPassword)) {
   echo "Response: " . $client->sendCommand($execCommand);
} else {
   $client->disconnect();
}