# swoole-worker **Repository Path**: FEIGE/swoole-worker ## Basic Information - **Project Name**: swoole-worker - **Description**: 基于swoole_process的workerman,支持毫秒级定时器,事件驱动,常驻进程,支持异步http,redis,mysql客户端,支持tcp,websocket,http等server - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 54 - **Forks**: 9 - **Created**: 2017-07-21 - **Last Updated**: 2024-09-06 ## Categories & Tags **Categories**: web-dev-toolkits **Tags**: None ## README # swoole-worker [![Join the chat at https://gitter.im/swoole-worker/Lobby](https://badges.gitter.im/swoole-worker/Lobby.svg)](https://gitter.im/swoole-worker/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) ## 关于本项目 此项目是workerman(v3.4.5)的swoole移植版本,移除了对pcntl,libevent,event,ev扩展的依赖,转而使用swoole提供的swoole_process和swoole_event,定时器采用swoole的swoole_timer,server采用stream扩展 ## 环境依赖 * PHP版本大于等于 5.4 (推荐PHP7及以上版本) * POSIX操作系统(Linux, OSX, BSD) * Posix和Swoole扩展 * Swoole扩展版本不小于1.9.18或者不小于2.0.8(2.0.10及其以上版本支持协程) ## 致谢 * [workerman](https://github.com/walkor/Workerman) Workerman是一款纯PHP开发的开源高性能的PHP socket 服务器框架 * [swoole](https://github.com/swoole/swoole-src) PHP的异步、并行、高性能网络通信引擎,使用纯C语言编写 * Workerman文档地址 http://doc.workerman.net/ * Swoole文档地址 https://wiki.swoole.com/wiki/index ## 安装 ``` composer require fage1151/swoole-worker ``` ## 基本用法 用法与workerman兼容 ### 协程用法 ```php connect('127.0.0.1', 6379); $ret = $redis->incr('coroutine'); $redis->close(); Swoole\Coroutine::create(function() { $redis = new Swoole\Coroutine\Redis(); $res = $redis->connect('127.0.0.1', 6379); $ret = $redis->set('coroutine_i', 50); $redis->close(); }); }); ``` ### 创建进程服务 ```php onWorkerStart = function($task) { // 每2.5秒执行一次 $time_interval = 2.5; Timer::add($time_interval, function() { echo "task run\n"; }); }; // 运行worker Worker::runAll(); ``` ### 创建websocket服务 ```php count = 4; // Emitted when new connection come $ws_worker->onConnect = function($connection) { echo "New connection\n"; }; // Emitted when data received $ws_worker->onMessage = function($connection, $data) { // Send hello $data $connection->send('hello ' . $data); }; // Emitted when connection closed $ws_worker->onClose = function($connection) { echo "Connection closed\n"; }; // Run worker Worker::runAll(); ``` ### 创建http服务 ```php count = 4; // Emitted when data received $http_worker->onMessage = function($connection, $data) { // $_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, $_FILES are available var_dump($_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, $_FILES); // send data to client $connection->send("hello world \n"); }; // run all workers Worker::runAll(); ``` ### 创建WebServer ```php count = 4; // Set the root of domains $web->addRoot('www.your_domain.com', '/your/path/Web'); $web->addRoot('www.another_domain.com', '/another/path/Web'); // run all workers Worker::runAll(); ``` ### 创建tcp server ```php count = 4; // Emitted when new connection come $tcp_worker->onConnect = function($connection) { echo "New Connection\n"; }; // Emitted when data received $tcp_worker->onMessage = function($connection, $data) { // send data to client $connection->send("hello $data \n"); }; // Emitted when new connection come $tcp_worker->onClose = function($connection) { echo "Connection closed\n"; }; Worker::runAll(); ``` ### 开启 SSL. ```php array( 'local_cert' => '/your/path/of/server.pem', 'local_pk' => '/your/path/of/server.key', ) ); // Create a Websocket server with ssl context. $ws_worker = new Worker("websocket://0.0.0.0:2346", $context); // Enable SSL. WebSocket+SSL means that Secure WebSocket (wss://). // The similar approaches for Https etc. $ws_worker->transport = 'ssl'; $ws_worker->onMessage = function($connection, $data) { // Send hello $data $connection->send('hello ' . $data); }; Worker::runAll(); ``` ### 自定义协议 Protocols/MyTextProtocol.php ```php onConnect = function($connection) { echo "New connection\n"; }; $text_worker->onMessage = function($connection, $data) { // send data to client $connection->send("hello world \n"); }; $text_worker->onClose = function($connection) { echo "Connection closed\n"; }; // run all workers Worker::runAll(); ``` ### 创建定时器 ```php onWorkerStart = function($task) { // 2.5 seconds $time_interval = 2.5; $timer_id = Timer::add($time_interval, function() { echo "Timer run\n"; } ); }; // run all workers Worker::runAll(); ``` ### 异步tcp客户端 (支持tcp/ws/text/frame...) ```php onWorkerStart = function() { // Websocket protocol for client. $ws_connection = new AsyncTcpConnection("ws://echo.websocket.org:80"); $ws_connection->onConnect = function($connection){ $connection->send('hello'); }; $ws_connection->onMessage = function($connection, $data){ echo "recv: $data\n"; }; $ws_connection->onError = function($connection, $code, $msg){ echo "error: $msg\n"; }; $ws_connection->onClose = function($connection){ echo "connection closed\n"; }; $ws_connection->connect(); }; Worker::runAll(); ``` ### 异步 Tcp 客户端 ```php onWorkerStart = function (Worker $worker) { $url = 'www.workerman.net:80'; $tcp = new Tcp($url); $tcp->onConnect = function ($client) { $client->send('123'); }; $tcp->onReceive = function ($client,$data) { var_dump($data); }; $tcp->connect(); }; $worker->count = 1; Worker::$stdoutFile = '/tmp/oauth.log'; Worker::$logFile = __DIR__ . '/workerman.log'; Worker::$pidFile = __DIR__ . "/" . str_replace('/', '_', __FILE__) . ".pid"; // 运行所有服务 Worker::runAll(); ``` ### 异步 WebSocket 客户端 ```php onWorkerStart = function (Worker $worker) { $url = 'laychat.workerman.net:9292'; $tcp = new Ws($url); $tcp->onConnect = function (Client $client) { var_dump($client); }; $tcp->onMessage = function (Client $client,$data) { $client->push('{"type":"ping"}'); var_dump($data); }; $tcp->connect(); }; $worker->count = 1; Worker::$stdoutFile = '/tmp/oauth.log'; Worker::$logFile = __DIR__ . '/workerman.log'; Worker::$pidFile = __DIR__ . "/" . str_replace('/', '_', __FILE__) . ".pid"; // 运行所有服务 Worker::runAll(); ``` ### 异步 Dns 客户端 ```php onWorkerStart = function() { swoole_async_dns_lookup("www.baidu.com", function($host, $ip){ echo "{$host} : {$ip}\n"; }); }; $worker->onMessage = function($connection, $host) { }; Worker::runAll(); ``` ### 异步 Http 客户端 https网站需要依赖openssl,必须在编译swoole时启用--enable-openssl ```php onWorkerStart = function (Worker $worker) { $url = 'http://www.workerman.net'; $request_method = 'get'; $data = ['uid'=>1]; $http = new Http($url, $request_method); $http->onResponse = function ($cli) { var_dump($cli->body); }; $http->request($data); }; $worker->count = 1; Worker::$stdoutFile = '/tmp/oauth.log'; Worker::$logFile = __DIR__ . '/workerman.log'; Worker::$pidFile = __DIR__ . "/" . str_replace('/', '_', __FILE__) . ".pid"; // 运行所有服务 Worker::runAll(); ``` ### 异步 Mysql 客户端 ```php onWorkerStart = function () { global $mysql; $mysql = new Mysql; $server = array( 'host' => '192.168.56.102', 'port' => 3306, 'user' => 'test', 'password' => 'test', 'database' => 'test', 'charset' => 'utf8', //指定字符集 'timeout' => 2, // 可选:连接超时时间(非查询超时时间),默认为SW_MYSQL_CONNECT_TIMEOUT(1.0) ); $mysql->connect($server, function (Mysql $db, $r) { if ($r === false) { var_dump($db->connect_errno, $db->connect_error); die; } }); }; $worker->onMessage = function ($connection, $data) { global $mysql; $sql = 'show tables'; $mysql->query($sql, function (Mysql $db, $r) { if ($r === false) { var_dump($db->error, $db->errno); } elseif ($r === true) { var_dump($db->affected_rows, $db->insert_id); } var_dump($r); }); }; Worker::runAll(); ``` ### 异步 Redis 客户端 ``` 使用Redis客户端,需要安装hiredis库 编译swoole时,在configure指令中加入--enable-async-redis ``` Document https://wiki.swoole.com/wiki/page/p-redis.html ```php onWorkerStart = function () { global $client; $client = new Redis; $client->connect('127.0.0.1', 6379, function (Redis $client, $result) { echo "connect\n"; var_dump($result); $db = 0; $client->select($db); $password = '111111'; $client->auth($password); }); }; $worker->onMessage = function ($connection, $data) { global $client; $client->set('key', 'swoole', function (Redis $client, $result) { var_dump($result); $client->get('key', function (Redis $client, $result) { var_dump($result); $client->close(); }); }); }; Worker::runAll(); ``` ### 异步 Zmq 客户端 安装: ``` apt-get install libzmq-dev pecl install zmq composer require swoole/zmq ``` ```php onWorkerStart = function () { $zmq = new Swoole\Async\ZMQ(); $zmq->connect('tcp://0.0.0.0:5555'); Swoole\Timer::tick(1000, function () use ($zmq) { static $i = 0; $msg = "hello-" . $i++; echo "Sending: $msg\n"; $zmq->send($msg); }); }; $worker->onMessage = function ($connection, $data) { }; Worker::runAll(); ``` ### 文档 IDE自动提示工具 https://github.com/eaglewu/swoole-ide-helper Swoole官方网站 https://wiki.swoole.com/wiki/index/prid-1 Workerman手册 http://doc.workerman.net/ ## 使用命令 以debug模式启动 ```php test.php start ``` 以daemon模式启动 ```php test.php start -d ``` ![workerman start](http://www.workerman.net/img/workerman-start.png) 查看进程状态 ```php test.php status ``` ![workerman satus](http://www.workerman.net/img/workerman-status.png?a=123) 停止运行 ```php test.php stop ``` 重新启动 ```php test.php restart ``` 平滑重启 ```php test.php reload ```