PHP code example of tourze / workerman-proxy-protocol
1. Go to this page and download the library: Download tourze/workerman-proxy-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/ */
tourze / workerman-proxy-protocol example snippets
use Workerman\Worker;
use Tourze\Workerman\ProxyProtocol\ProxyProtocolV1;
use Tourze\Workerman\ProxyProtocol\HeaderMap;
// Create Worker instance
$worker = new Worker('tcp://0.0.0.0:8080');
// Use Proxy Protocol V1
$worker->protocol = ProxyProtocolV1::class;
// Handle incoming messages with proxy protocol information
$worker->onMessage = function($connection, $data) {
// Check if proxy protocol header was parsed
if (HeaderMap::has($connection)) {
$header = HeaderMap::get($connection);
// Get client's original IP and port
$sourceIp = $header->getSourceIp();
$sourcePort = $header->getSourcePort();
echo "Received data from {$sourceIp}:{$sourcePort}: " . $data . "\n";
// Respond to client
$connection->send("Your IP is {$sourceIp}, port is {$sourcePort}\n");
} else {
echo "Received data without proxy protocol header: " . $data . "\n";
$connection->send("Could not get your real IP\n");
}
};
// Run worker
Worker::runAll();
use Workerman\Worker;
use Tourze\Workerman\ProxyProtocol\ProxyProtocolV2;
use Tourze\Workerman\ProxyProtocol\HeaderMap;
// Create Worker instance
$worker = new Worker('tcp://0.0.0.0:8080');
// Use Proxy Protocol V2
$worker->protocol = ProxyProtocolV2::class;
// Message handling logic is the same as V1
$worker->onMessage = function($connection, $data) {
if (HeaderMap::has($connection)) {
$header = HeaderMap::get($connection);
// Handle message...
}
};
Worker::runAll();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.