1、composer安装tp5的swoole插件
composer require topthink/think-worker
2、WorkerController控制器
namespace app\index\controller;
use think\worker\Server;
use Workerman\Connection\TcpConnection;
class WorkerController extends Server {
protected $socket = 'http://0.0.0.0:2345';
protected $processes = 1;
/**
* @param TcpConnection $connection
* @param $data
*/
function onMessage($connection, $data) {
$connection->send("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
}
}
3、启动文件startWorker.php
define('APP_PATH', __DIR__ . '/../application/');
define('BIND_MODULE','index/Worker');
require __DIR__ . '/../thinkphp/start.php';
4、启动workerman服务
php startWorker.php start
php startWorker.php start -d
5、浏览器访问
http://127.0.0.1:2345/
6、ab压测
ab -c100 -n10000 -k http://127.0.0.1:2345/