PHP code example of tourze / workerman-relay-worker
1. Go to this page and download the library: Download tourze/workerman-relay-worker 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-relay-worker example snippets
use Tourze\Workerman\ConnectionPipe\Enum\ProtocolFamily;
use Tourze\Workerman\ConnectionPipe\Model\Address;
use Tourze\Workerman\RelayWorker\RelayWorker;
use Workerman\Connection\ConnectionInterface;
// Create a relay worker listening on a TCP port
$worker = new RelayWorker('tcp://0.0.0.0:8080');
// Set onConnect callback to establish the relay target
$worker->onConnect = function(ConnectionInterface $connection) {
// Define target address for the connection
$address = Address::create('127.0.0.1', 9000, ProtocolFamily::TCP);
RelayWorker::addTarget($connection, $address);
};
// Start the worker
Worker::runAll();
use Tourze\Workerman\ConnectionPipe\Enum\ProtocolFamily;
use Tourze\Workerman\ConnectionPipe\Model\Address;
use Tourze\Workerman\RelayWorker\LoadBalancer\LoadBalancerFactory;
use Tourze\Workerman\RelayWorker\RelayWorker;
use Workerman\Connection\ConnectionInterface;
$worker = new RelayWorker('tcp://0.0.0.0:8080');
// Define multiple target servers
$targets = [
Address::create('192.168.1.10', 9000, ProtocolFamily::TCP),
Address::create('192.168.1.11', 9000, ProtocolFamily::TCP),
Address::create('192.168.1.12', 9000, ProtocolFamily::TCP),
];
$worker->onConnect = function(ConnectionInterface $connection) use ($targets) {
// Set multiple targets and load balancer
RelayWorker::setTargets($connection, $targets);
// Use round robin load balancer
$loadBalancer = LoadBalancerFactory::createRoundRobin();
RelayWorker::setLoadBalancer($connection, $loadBalancer);
};
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.