PHP code example of tourze / workerman-chain-protocol
1. Go to this page and download the library: Download tourze/workerman-chain-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-chain-protocol example snippets
use Tourze\Workerman\ChainProtocol\ChainProtocol;
use Tourze\Workerman\ChainProtocol\Container;
use Workerman\Worker;
// Setup your custom protocol classes that implement ProtocolInterface
Container::$decodeProtocols = [
MyProtocol1::class,
MyProtocol2::class,
// Add more protocols as needed
];
Container::$encodeProtocols = [
MyProtocol2::class,
MyProtocol1::class,
// The order matters for encoding (reverse of decoding)
];
// Setup logger if needed
Container::$logger = new MyLogger();
// Setup event dispatcher if needed
Container::$eventDispatcher = new MyEventDispatcher();
// Create a Workerman server with ChainProtocol
$worker = new Worker('tcp://0.0.0.0:8080');
$worker->protocol = ChainProtocol::class;
$worker->onMessage = function($connection, $data) {
// $data is already decoded through all protocols in the chain
// Process your business logic here
// Send a response, which will be encoded through all protocols in the chain
$connection->send('Your response');
};
Worker::runAll();