-
Notifications
You must be signed in to change notification settings - Fork 0
PHP Edition
This tutorial explains how to use the PHP version of ServerWebGamePost. You need at least PHP 5.4 to take advantage of features like short array syntax and callable type hinting.
No additional dependencies are required if you work in a PHP environment with cURL enabled. Simply integrate the classes into your project.
Define a callback function to handle the received datapackets:
<?php
function processDatapacks(array $datapack) {
// Process the datapack (associative array)
// Example: log the data, update a database, etc.
}
?>
Create an instance of the Server class by specifying the port, the image path (can be null), and the callback to process datapackets:
<?php
// filepath: your/path/to/server_setup.php
require_once 'Server.php';
$port = 8080; // Port on which the server will listen
$imgSrc = 'path/to/icon.jpg'; // Path to the server image (can be null)
$server = new Server($port, $imgSrc, 'processDatapacks');
// Process the incoming request (this acts as your endpoint)
// This method should be invoked in the PHP file that serves as the endpoint (e.g., index.php)
$server->processSubDatapacks();
?>
Use the banIp and unbanIp functions to manage blocked IPs:
<?php
// Ban a specific IP
$server->banIp('192.168.1.100');
// Unban the IP
$server->unbanIp('192.168.1.100');
?>
If you want to restrict allowed origins, use addFilterOrigin and removeFilterOrigin. If you do not configure any filters, the default origin header will be "*":
<?php
// Add an allowed origin (the request header "Origin" must match)
$server->addFilterOrigin('example.com');
// Remove a previously added origin
$server->removeFilterOrigin('example.com');
?>
In PHP, execution typically ends after responding to the request. If you implement a persistent server, you will need to add your own logic to stop it.
Define a callback function to process datapackets received by the client:
<?php
function clientProcessDatapacks(array $datapack) {
// Process the datapack received by the client
}
?>
Create an instance of the Client class by providing the domain, port, and whether to use HTTPS. Set the callback and send a datapacket (remember to include the identifier key in the object):
<?php
// filepath: your/path/to/client_setup.php
require_once 'Client.php';
$domain = 'localhost'; // Server domain or IP
$port = 8080; // Server port
$isHttps = false; // Change to true if using HTTPS
$client = new Client($domain, $port, $isHttps);
$client->setProcessDatapacks('clientProcessDatapacks');
// Example datapacket
$datapacket = [
'identifier' => 'client1', // Client identifier
'data' => 'Example data'
];
$client->sendDatapacket($datapacket);
?>
PHP itself does not include an HTTP server with built-in HTTPS support like Node.js does, so you must configure HTTPS using Apache, Nginx, or a similar server. If you configure your server to use HTTPS, be sure to set the $isHttps parameter to true when creating the Client instance.
- Server: Set up a Server instance with a port, an optional image path, and a callback for processing datapackets.
- Client: Set up a Client instance with the domain, port, and HTTPS flag; assign the processing callback and send datapackets using sendDatapacket.
- Security and Filtering: Use functions to ban IPs or filter origins as required.
Note
Make sure your PHP environment is at least version 5.4 so that the features used in the code are supported.