PHP code example of paulxu-cn / co-workerman
1. Go to this page and download the library: Download paulxu-cn/co-workerman 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/ */
paulxu-cn / co-workerman example snippets
// file: ./simpleTimerServer.php
define('DS', DIRECTORY_SEPARATOR);
nnection\CoTcpConnection;
$worker = new CoWorker('tcp://0.0.0.0:8686');
$worker->onConnect = function (CoTcpConnection $connection) {
echo "New Connection, {$connection->getLocalIp()}" . PHP_EOL;
yield from CoTimer::sleepAsync(1000); // yield, and go back after 1 second.
$re = yield from $connection->sendAsync('hello');
CoWorker::safeEcho( "get re: [{$re}]" . PHP_EOL);
};
// 运行worker
CoWorker::runAll();
// file: ~/examples/example5/coCartServer.php
define('DS', DIRECTORY_SEPARATOR);
n\CoTcpClient;
use CoWorkerman\Connection\CoTcpConnection;
$worker = new CoWorker('tcp://0.0.0.0:8686');
$worker->onConnect = function (CoTcpConnection $connection) {
echo "New Connection, {$connection->getLocalIp()} \n";
$re = checkInventoryAsync($connection, rand(10, 20), true);
$re2 = checkProductAsync($connection, rand(10, 20), true);
// 顺序异步执行 ------------------------------------------------------------------+
// $re = yield from Promise::wait($re, 'onConnect');
// $re2 = yield from Promise::wait($re2, 'onConnect');
// or 同时异步执行 ---------------------------------------------------------------+
list($re, $re2) = yield from Promise::all(array($re, $re2), 'onConnect');
// 选择结束 ----------------------------------------------------------------------+
if (isset($re['re']) && isset($re2['re'])) {
$check = $re['re'] && $re2['re'];
}
$connection->sendAsync($check);
};
/**
* 发起rpc检查统一接口
*
* @param $host
* @param $port
* @param $method
* @param $data
* @param bool $noBlocking
* @return bool|false|string
*/
function checkClientAsync($connection, $host, $port, $method, $data, $noBlocking = true)
{
CoWorker::safeEcho('try to connect to ' . "tcp://$host:$port" . PHP_EOL);
$con = new CoTcpClient("tcp://$host:$port");
$ifCon = $con->connectAsync($connection);
$ifCon = yield from Promise::wait($ifCon, __FUNCTION__);
if (!$ifCon) {
return null;
}
CoWorker::safeEcho( "\nconnected to server, re: [{$ifCon}]\n" . PHP_EOL);
$message = json_encode(["method" => $method, "data" => $data]);
$re = $con->sendAsync($message);
return $re;
}
function checkInventoryAsync($connection, $productId, $noBlocking = true)
{
$host = "127.0.0.1";
$port = 8081;
$data = array('productId' => $productId);
return yield from checkClientAsync($connection, $host, $port, 'inventory', $data, $noBlocking);
}
function checkProductAsync($connection, $productId, $noBlocking = true)
{
$host = "127.0.0.1";
$port = 8082;
$data = array('productId' => $productId);
return yield from checkClientAsync($connection, $host, $port, 'product', $data, $noBlocking);
}
// 运行worker
CoWorker::runAll();
// file: ~/example/example3/coWorkerMySQLtest1.php
ion;
$worker = new CoWorker('tcp://0.0.0.0:6161');
// 收到客户端请求时
$worker->onMessage = function($connection, $data) {
global $mysql;
$mysql = new Connection(array(
'host' => '127.0.0.1', // 不要写localhost
'dbname' => 'mysql',
'user' => 'root',
'password' => '123456',
'port' => '3306'
));
$connected = yield from $mysql->connection();
$re = yield from $mysql->query('show databases');
CoWorker::safeEcho(json_encode($re) . PHP_EOL);
};
CoWorker::runAll();
shell script
## start the CoWorkerman Server
$ php ./simpleTimerServer.php start
## talnet it
$ talnet 127.0.0.1:8686
> hi
hello
shell script
## 启动一个处理耗时2s的库存服务
$ php ./examples/example5/otherServerFork.php 8081 inventory 1
## 启动一个处理耗时4s的产品服务
$ php ./examples/example5/otherServerFork.php 8082 product 2
## 监听8083端口,处理一个请求 耗时6s的 promo 服务
$ php ./examples/example5/otherServerFork.php 8083 promo 3
shell script
## 启动一个非阻塞购物车服务
$ php ./coCartServer.php
## 客户端请求
$ php ./examples/example5/userClientFork.php
shell script
## start the MySQL client server
$ php ./examples/example3/coWorkerMySQLtest1.php start