Sponsor
You need to ensure that the operating environment meets the following requirements:
composer create-project api-swoole/skeleton
Supports HTTP service, WebSocket service, tcp service, and executes commands in the project root directory ./apiswoole.php
.
php apiswoole.php
In the api-swoole framework, the main business code is in the app directory. Each namespace inside corresponds to a subdirectory. The default namespace of the project is App. After the project is created, the app directory contains three subdirectories: Common, Example, and Ext. The Common directory stores the functions.php file of the function, and Ext generally places tools, etc. The directory structure is as follows:
./
└── app
├── Example # 放置接口源代码,相当于控制器层
├── Common # 公共代码目录,
└── Ext# 放置工具等
When the project needs to add a new interface, first create a new hello.php
file in the ./app/Example
directory and edit the code with an editor.
<?php
namespace AppExample;
use SapiApi;
class Hello extends Api
{
public function index()
{
return [
'code' => 200,
'data' => [
'name' => 'api-swoole',
'version' => DI()->config->get('conf.version'),
],
];
}
}
Define the project route in the route/http.php
routing file. The first parameter is the defined browser access address. The first half of the second parameter @
is the complete namespace of the file. The second half of @
is the specific name called in the class. method.
return [
HttpRouter("/hello", "AppExampleHello@index"),
];
Execute the following command in the project root directory to start in non-daemon mode.
php apiswoole.php
By default, it listens to the local HTTP and websocket port 9501. The following output information appears in cmd to indicate that the project is started successfully.
[Success] Swoole: 4.5.9, PHP: 7.4.13, Port: 9501
[Success] Swoole Http Server running:http://0.0.0.0:9501
[Success] Swoole websocket Server running:ws://0.0.0.0:9501
[Success] Swoole tcp Server running:0.0.0.0:9500
[Success] Swoole udp Server running:0.0.0.0:9502
Enter the defined routing address in the browser address bar, and the address consists of http://ip网址:端口/定义的路由
.
http://127.0.0.1:9501/hello
The data returned after a successful request is returned in json format by default, including the default code, msg, and data fields. The data in data is the data returned in the method.
{
"code" : 200 ,
"msg" : " success " ,
"data" : {
"code" : 200 ,
"data" : {
"name" : " api-swoole " ,
"version" : " 1.0.2 "
}
},
"debug" : []
}
Since HttpServer
's support for the HTTP
protocol is incomplete, it is recommended to use it only as an application server to handle dynamic requests, and to add Nginx
as a proxy on the front end. (reference address)
server {
listen 80;
server_name swoole.test;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:9501;
}
}
You can get the client's real IP
by reading $request->header['x-real-ip']
The default config folder contains three files: conf.php, db.php, and events.php. conf.php places the configuration information of the project, including http, tcp, udp, websocket and other configurations. db.php is responsible for configuring MySQL and Redis connections, and events.php is responsible for defining and customizing specific event registration and monitoring. It now supports workerStart、open、close、task、finish
event registration. Configuration information can be read through DI()->config->get('conf.ws')
or SapiDi::one()->config->get('conf.ws')
.
./config/
├── conf.php #http、tcp、udp、websocket等配置
├── db.php #数据库配置
└── events.php #定制特定事件注册监听
<?php
return [
'version' => '1.0.2',
'debug' => true,//调试模式
'log' => [
'displayConsole' => true,//true控制台打印日志
'saveLog' => true,//保存日志
],
'udp' => [
'host' => '0.0.0.0',
'port' => 9502,
'sockType' => SWOOLE_SOCK_UDP,
'events' => [
['packet', AppExampleUdpServe::class, 'onPacket'],
],
'settings' => [],
],
'tcp' => [
'host' => '0.0.0.0',
'port' => 9501,
'sockType' => SWOOLE_SOCK_TCP,
'events' => [
['Receive', AppExampleTcpServe::class, 'onReceive'],
['connect', AppExampleTcpServe::class, 'onConnect'],
['close', AppExampleTcpServe::class, 'onClose'],
],
'settings' => [],
],
'ws' => [
'host' => '0.0.0.0',
'port' => 9500,
'events' => [
['open', SapiEvents::class, 'onOpen'],
['message', SapiEvents::class, 'onMessage'],
['close', SapiEvents::class, 'onClose'],
['request', SapiEvents::class, 'onRequest'],
['Task', SapiEvents::class, 'onTask'],
['Finish', SapiEvents::class, 'onFinish'],
['workerStart', SapiEvents::class, 'onWorkerStart'],
['start', SapiEvents::class, 'onStart'],
],
'settings' => [
'daemonize' => false,//设置 daemonize => true 时,程序将转入后台作为守护进程运行。长时间运行的服务器端程序必须启用此项。如果不启用守护进程,当 ssh 终端退出后,程序将被终止运行
// 'dispatch_mode' => 2,//数据包分发策略。【默认值:2】
'worker_num' => swoole_cpu_num(),
'log_file' => 'storage/swoole',
'log_rotation' => SWOOLE_LOG_ROTATION_DAILY,
'log_date_format' => '%Y-%m-%d %H:%M:%S',
'log_level' => SWOOLE_LOG_DEBUG,
'task_worker_num' => 10,
'enable_coroutine' => true,//是否启用异步风格服务器的协程支持
// 'buffer_output_size' => 32 * 1024 * 1024, //配置发送输出缓存区内存尺寸。【默认值:2M】
// 'document_root' => ROOT_PATH,
// 'enable_static_handler' => true,//开启静态文件请求处理功能
// 'static_handler_locations' => ['/chatroom', '/app/images'],//设置静态处理器的路径。类型为数组,默认不启用。
],
// SWOOLE_LOG_DEBUG 调试日志,仅作为内核开发调试使用
// SWOOLE_LOG_TRACE 跟踪日志,可用于跟踪系统问题,调试日志是经过精心设置的,会携带关键性信息
// SWOOLE_LOG_INFO 普通信息,仅作为信息展示
// SWOOLE_LOG_NOTICE 提示信息,系统可能存在某些行为,如重启、关闭
// SWOOLE_LOG_WARNING 警告信息,系统可能存在某些问题
// SWOOLE_LOG_ERROR 错误信息,系统发生了某些关键性的错误,需要即时解决
// SWOOLE_LOG_NONE 相当于关闭日志信息,日志信息不会抛出
],
'process' => [
[AppExampleProcess::class, 'addProcess']
],//添加用户自定义的工作进程
];
The service has been registered by default in apiswoole.php
when the project is started.
$di = DI();
$di->config = new Config("./config");
For example, the configuration information is conf.php, and the structure is:
return [
'debug' => true,//调试模式
'tcp' => [
'host' => '0.0.0.0',
'port' => 9500,
'sockType' => SWOOLE_SOCK_TCP,
'events' => [
['receive', AppControllerTcpServe::class, 'onReceive'],
],
'settings' => [],
],
]
You can use DI()->config->get('conf.tcp')
to read the configuration file
DI()->config->get('conf.tcp') #返回数组
Returns an array data structure:
{
"host" : " 0.0.0.0 " ,
"port" : 9500 ,
"sockType" : 1 ,
"events" : [
[
" receive " ,
" App \ Controller \ TcpServe " ,
" onReceive "
]
],
"settings" : []
}
The log interface is defined in detail according to the PSR specification. Logs are recorded in the ./storage/log
directory, and日期.log
are generated daily. Currently the following types of logging are supported:
Logging system uses:
DI()->logger->debug("日志测试debug"); #开发调试类日记
DI()->logger->info("日志测试info"); #业务纪录类日记
DI()->logger->notice("日志测试notice");#系统提示类日记
DI()->logger->waring("日志测试waring");#系统致命类日记
DI()->logger->error("日志测试error"); #系统异常类日记
The corresponding log in the ./storage/log/20220906.log
directory:
[swoole] | [2022-09-06 01:32:05] | debug | 日志测试debug
[swoole] | [2022-09-06 01:32:05] | info | 日志测试info
[swoole] | [2022-09-06 01:32:05] | notice | 日志测试notice
[swoole] | [2022-09-06 01:32:05] | warning | 日志测试waring
[swoole] | [2022-09-06 01:32:05] | error | 日志测试error
WebSocketServer
inherits from HttpServer, so all API
and configuration items provided by HttpServer
can be used. Please refer to the HttpServer chapter.
WebSocketServer
can also serve as an HTTP
server at the same time.WebSocketServer
will return an HTTP 400
error page after receiving the HTTP
request. If you only want to implement the websocket server, delete ['request', SapiEvents::class, 'onRequest']
in the ./config/conf.php
configuration.
HTTP/websocket server configuration options are in ./config/conf.php
. For specific configuration information, please refer to the Swoole document configuration options.
<?php
return [
'ws' => [
'host' => '0.0.0.0',//监听地址
'port' => 9501,//监听端口
'events' => [
['open', SapiEvents::class, 'onOpen'],
['message', SapiEvents::class, 'onMessage'],
['close', SapiEvents::class, 'onClose'],
['request', SapiEvents::class, 'onRequest'],//HTTP服务器回调
['Task', SapiEvents::class, 'onTask'],
['Finish', SapiEvents::class, 'onFinish'],
['workerStart', SapiEvents::class, 'onWorkerStart'],
['start', SapiEvents::class, 'onStart'],
],//回调函数
'settings' => [
'daemonize' => false,//设置 daemonize => true 时,程序将转入后台作为守护进程运行。长时间运行的服务器端程序必须启用此项。如果不启用守护进程,当 ssh 终端退出后,程序将被终止运行
'worker_num' => swoole_cpu_num(),
'log_file' => 'storage/swoole',
'log_rotation' => SWOOLE_LOG_ROTATION_DAILY,
'log_date_format' => '%Y-%m-%d %H:%M:%S',
'log_level' => SWOOLE_LOG_DEBUG,
'task_worker_num' => 10,
],
],
];
The route build file for the HTTP server is located in ./route/http.php
. Declare specific routing rules. The first parameter is the defined browser access address. The first half of the second parameter @
is the complete namespace of the file. The second half of @
is the specific method called in the class.
return [
HttpRouter("/", "AppExampleApp@Index"),
HttpRouter("/hello", "AppExampleHello@index"),
HttpRouter("声明浏览器地址", "命名空间+类@类中的方法"),
];
The complete URL address consists of http://ip网址:端口/定义的路由
.
Building a basic route only requires a URI and a闭包
, providing a very simple way to define routes:
return [
HttpRouter("/", function (SwooleHttpRequest $request, SwooleHttpResponse $response) {
return [
"code" => 200,
"msg" => "hello World!",
'tm' => date('Y-m-d H:i:s'),
"data" => [
'name' => 'api-swoole',
'version' => DI()->config->get('conf.version'),
],
];
}),
];
The corresponding controller method has two parameters, $request
and $reponse
, by default. For a complete introduction to the Request and Response objects, please view the Swoole documentation: HttpRequest, HttpResponse
<?php
namespace AppController;
use SapiApi;
class Hello extends Api
{
public function index()
{
return [
'code' => 200,
'data' => 'hello world'
];
}
}
Interface parameter rules are implemented by inheriting the Api
class, and specific definition rules are implemented through rule()
method.
name
corresponds to the value passed in by the clientrequire
indicates that the value is passed in as an option, true
must be passed in, false
must be passed in.type
, supports int
, string
, float
, bool
, array
, file
source
parameter is passed in to the location and supports get
, post
, and header
message
interface parameter prompt information <?php
namespace AppExample;
use SapiApi;
//字段验证
class App extends Api
{
public function rule()
{
return [
'Index' => [
'intValue' => ['name' => 'intValue', 'require' => true, 'type' => 'int', 'source' => 'post', 'message' => 'intValue必须携带'],
'stringValue' => ['name' => 'stringValue', 'require' => true, 'type' => 'string', 'source' => 'post', 'message' => 'stringValue必须携带'],
'floatValue' => ['name' => 'floatValue', 'require' => true, 'type' => 'float', 'source' => 'post', 'message' => 'floatValue是必须的'],
'boolValue' => ['name' => 'boolValue', 'require' => true, 'type' => 'bool', 'source' => 'post', 'message' => 'boolValue是必须的'],
'arrayValue' => ['name' => 'arrayValue', 'require' => true, 'type' => 'array', 'source' => 'post', 'message' => 'arrayValue是必须的'],
'file' => ['name' => 'file', 'require' => false, 'type' => 'file', 'ext' => 'jpeg,png,txt', 'source' => 'post', 'message' => 'file文件是必须的'],
'x-token' => ['name' => 'x-token', 'require' => true, 'type' => 'string', 'source' => 'header', 'message' => '缺少请求头x-token'],
]
];
}
public function Index(SwooleHttpRequest $request, SwooleHttpResponse $response): array
{
return [
"code" => 200,
"msg" => "hello World!",
'tm' => date('Y-m-d H:i:s'),
"data" => [
'name' => 'api-swoole',
'version' => DI()->config->get('conf.version'),
'intValue' => $request->post['intValue'],
'stringValue' => $request->post['stringValue'],
'floatValue' => $request->post['floatValue'],
'boolValue' => $request->post['boolValue'],
'arrayValue' => $request->post['arrayValue'],
'x-token' => $request->header['x-token'],
'file' => saveFile($request, './tmp/')
],
];
}
}
The route definition file of the websocket server is located in ./route/websocket.php
. Declare specific routing rules. The first parameter is the defined browser access address. The first half of the second parameter @
is the complete namespace of the file. The second half of @
is the specific method called in the class.
return [
WsRouter("/", "AppExampleWebsocket@index"),
WsRouter("/login", "AppExampleWebsocket@login"),
];
The complete URL address consists of ws://ip网址:端口
.
The corresponding controller method has two parameters $server
and $msg
, by default. For a complete introduction to the $server
object, please see the Swoole documentation: WebSocketServer. $msg
is the data information sent by the client.
The data information sent by the client needs to be transmitted in json format by default and must contain the three fields of id, path, and data.
id
field is the unique identifier of the message body.path
field is the access address declared by the ./route/websocket.php
route.data
field is the specific message parameter of the project, and the default control method is $msg
. {
"id" : " 918wsEMQDrj0RXxm " ,
"path" : " / " ,
"data" : {
"username" : " api-swoole "
}
}
Example of controller code section:
<?php
namespace AppExample;
use SapiApi;
class WebsocketHello extends Api
{
public function hello(SwooleWebSocketServer $server, array $msg): array
{
return [
'err' => 200,
'data' => [
'name' => 'api-swoole',
'version' => DI()->config->get('conf.version'),
]
];
}
}
Interface parameter rules are implemented by inheriting the Api
class, and specific definition rules are implemented through rule()
method.
name
corresponds to the value passed in by the clientrequire
indicates that the value is passed in as an option, true
must be passed in, false
must be passed in.type
type, supports int
, string
, float
, bool
, array
message
interface parameter prompt information <?php
namespace AppExample;
class Websocket extends WsBase
{
public function rule()
{
return [
'Index' => [
'intValue' => ['name' => 'intValue', 'require' => true, 'type' => 'int', 'message' => 'intValue必须携带'],
'stringValue' => ['name' => 'stringValue', 'require' => true, 'type' => 'string', 'message' => 'stringValue必须携带'],
'floatValue' => ['name' => 'floatValue', 'require' => true, 'type' => 'float', 'message' => 'floatValue是必须的'],
'boolValue' => ['name' => 'boolValue', 'require' => true, 'type' => 'bool', 'message' => 'boolValue是必须的'],
'arrayValue' => ['name' => 'arrayValue', 'require' => true, 'type' => 'array', 'message' => 'arrayValue是必须的'],
'x-token' => ['name' => 'x-token', 'require' => true, 'type' => 'string', 'message' => '缺少请求头x-token'],
]
];
}
public function Index(SwooleWebSocketServer $server, array $msg): array
{
return [
'err' => 200,
'data' => [
'intValue' => $msg['intValue'],
'stringValue' => $msg['stringValue'],
'floatValue' => $msg['floatValue'],
'boolValue' => $msg['boolValue'],
'arrayValue' => $msg['arrayValue'],
'x-token' => $msg['x-token'],
]
];
}
}
The Api
class has a built-in hook function userCheck
, and all HTTP controllers can inherit the Api
class overload. For example, user authentication can be accomplished.
First define the declaration HttpBase.php
file.
<?php
namespace AppExample;
use SapiApi;
class HttpBase extends Api
{
//用户权限验证
public function userCheck(SwooleHttpRequest $request): string
{
if ($request->header["x-token"] != "123123") {
return "token过期";
}
return "";
}
}
Then inherit HttpBase.php
to implement class overloading.
<?php
namespace AppExample;
class Auth extends HttpBase
{
public function rule()
{
return [
'login' => [
'username' => ['name' => 'username', 'require' => true, 'type' => 'string', 'source' => 'post', 'message' => '必须携带username'],
]
];
}
public function login(SwooleHttpRequest $request, SwooleHttpResponse $response): array
{
return [
"code" => 200,
"msg" => "login",
"data" => [
'username' => $request->post['username']
]
];
}
}
The Api
class has a built-in hook function userWsCheck
, and all websocket controllers can inherit Api
class overload. For example, user authentication can be accomplished.
First define the declaration WsBase.php
file.
<?php
namespace AppExample;
use SapiApi;
class WsBase extends Api
{
public function userWsCheck(SwooleWebSocketFrame $frame): string
{
$res = json_decode($frame->data, true);
if (!isset($res['data']["x-token"]) || $res['data']["x-token"] != "123123") {
return "token expired";
}
return "";
}
}
Then inherit WsBase.php
to implement class overloading.
<?php
namespace AppExample;
class Websocket extends WsBase
{
public function rule()
{
return [
'Index' => [
'intValue' => ['name' => 'intValue', 'require' => true, 'type' => 'int', 'message' => 'intValue必须携带'],
'stringValue' => ['name' => 'stringValue', 'require' => true, 'type' => 'string', 'message' => 'stringValue必须携带'],
'floatValue' => ['name' => 'floatValue', 'require' => true, 'type' => 'float', 'message' => 'floatValue是必须的'],
'boolValue' => ['name' => 'boolValue', 'require' => true, 'type' => 'bool', 'message' => 'boolValue是必须的'],
'arrayValue' => ['name' => 'arrayValue', 'require' => true, 'type' => 'array', 'message' => 'arrayValue是必须的'],
'x-token' => ['name' => 'x-token', 'require' => true, 'type' => 'string', 'message' => '缺少请求头x-token'],
]
];
}
public function Index(SwooleWebSocketServer $server, array $msg): array
{
return [
'err' => 200,
'data' => [
'intValue' => $msg['intValue'],
'stringValue' => $msg['stringValue'],
'floatValue' => $msg['floatValue'],
'boolValue' => $msg['boolValue'],
'arrayValue' => $msg['arrayValue'],
'x-token' => $msg['x-token'],
]
];
}
}
Server
can listen to multiple ports, and each port can be set to handle different protocols. For example, port 80 handles HTTP protocol, port 9500 handles TCP protocol, and port 9502 handles UDP protocol. SSL/TLS
transport encryption can also be enabled only for specific ports. Refer to Swoole official documentation (multi-port monitoring)
TCP server configuration options add the tcp field in ./config/conf.php
. For specific configuration information, please refer to the Swoole document TCP configuration.
'tcp' => [
'host' => '0.0.0.0',
'port' => 9500,
'sockType' => SWOOLE_SOCK_TCP,
'events' => [
['receive', AppControllerTcpServe::class, 'onReceive'],//TCP服务器回调
],
'settings' => [],
],
UDP server configuration options add the udp field in ./config/conf.php
. For specific configuration information, please refer to the Swoole document UDP configuration.
'udp' => [
'host' => '0.0.0.0',
'port' => 9502,
'sockType' => SWOOLE_SOCK_UDP,
'events' => [
['packet', AppControllerUdpServe::class, 'onPacket'],//UDP服务器回调
],
'settings' => [],
],
The Swoole development team uses Hook's native PHP function to implement the coroutine client. With one line of code, the original synchronous IO code can be turned into asynchronous IO that can be scheduled by the coroutine, that is, one-click coroutineization. The SwooleServer class clusters provided by Swoole
are automatically created and do not need to be done manually. Please refer to enable_coroutine. For specific content, please refer to Swoole’s official website for one-click coroutineization.
The framework database introduces the simple-swoole
third-party expansion package. For details, please refer to simple-swoole/db.
Redis server configuration options are in ./config/db.php
.
<?php
return [
'redis' => [
'host' => '192.168.0.105',//Redis服务器地址
'port' => 6379,//指定 Redis 监听端口
'auth' => '',//登录密码
'db_index' => 2,//指定数据库
'time_out' => 1,//
'size' => 64,//连接池数量
],
];
Using Redis in the project
<?php
namespace AppController;
use AppExtRedis;
class RedisDemo
{
public function setData(SwooleHttpRequest $request, SwooleHttpResponse $response)
{
$redis = new SimpsDBBaseRedis();
$res = $redis->set('我是key', '我是value');
return [
"code" => 200,
"msg" => "hello World!",
"data" => [
'res' => $res,
'key' => $request->get['key'],
'val' => $request->get['val'],
],
];
}
}
MySQL server configuration options are in ./config/db.php
.
<?php
return [
'mysql' => [
'host' => '',//连接地址
'port' => ,//连接端口
'database' => '',//数据库名称
'username' => 'root',//用户
'password' => '',//密码
'charset' => 'utf8',//字符集
'unixSocket' => null,//
'options' => [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
],
'size' => 64 // 连接池数量
],
];
./app/Ext/Pool.php
configures the connection pool:
<?php
namespace AppExt;
use SapiSingleton;
use SimpsDBPDO;
use SimpsDBRedis;
class Pool
{
use Singleton;
public function startPool(...$args)
{
$mysql_config = DI()->config->get('db.mysql');
if (!empty($mysql_config)) {
PDO::getInstance($mysql_config);
}
$redis_config = DI()->config->get('db.redis');
if (!empty($redis_config)) {
Redis::getInstance($redis_config);
}
}
}
simple-swoole
integrates the lightweight PHP database framework Medoo. You need to inherit SimpsDBBaseModel
when using it, so the usage method is basically the same as Medoo. Please check Medoo's documentation for details.
The only difference is the transaction-related operations. In Medoo, action( $callback )
method is used, but in this framework, action( $callback )
method can also be used. In addition, the following methods are also supported
beginTransaction(); // 开启事务
commit(); // 提交事务
rollBack(); // 回滚事务
Transaction example
$this->beginTransaction();
$this->insert("user", [
"name" => "luffy",
"gender" => "1"
]);
$this->delete("user", [
"id" => 2
]);
if ($this->has("user", ["id" => 23]))
{
$this->rollBack();
} else {
$this->commit();
}
Used in the project, database table structure:
CREATE TABLE `user_info` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`nick` varchar(15) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8;
<?php
namespace AppController;
class MysqlDemo
{
public function getOne(SwooleHttpRequest $request, SwooleHttpResponse $response)
{
$uid = $request->post['uid'];
$database = new SimpsDBBaseModel();
$res = $database->select("user_info", [
"uid",
"nick",
], [
"uid" => $uid
]);
return [
"code" => 200,
"msg" => "MysqlDemo getOne",
"data" => [
'res' => $res,
'uid' => $uid,
],
];
}
public function save(SwooleHttpRequest $request, SwooleHttpResponse $response)
{
$username = $request->post['username'];
$database = new SimpsDBBaseModel();
$last_user_id = $database->insert("user_info", [
"uid" => time(),
"nick" => $username,
]);
return [
"code" => 200,
"msg" => "MysqlDemo save",
"data" => [
'last_user_id' => $last_user_id,
'username' => $username,
],
];
}
public function del(SwooleHttpRequest $request, SwooleHttpResponse $response)
{
$uid = $request->post['uid'];
$database = new SimpsDBBaseModel();
$res = $database->delete("user_info", [
"uid" => $uid
]);
return [
"code" => 200,
"msg" => "MysqlDemo del",
"data" => [
'res' => $res,
'uid' => $uid,
],
];
}
public function update(SwooleHttpRequest $request, SwooleHttpResponse $response)
{
$uid = $request->post['uid'];
$username = $request->post['username'];
$database = new SimpsDBBaseModel();
$res = $database->update("user_info", [
"nick" => $username
], [
"uid" => $uid
]);
return [
"code" => 200,
"msg" => "MysqlDemo update",
"data" => [
'res' => $res,
'uid' => $uid,
'username' => $username,
],
];
}
}
Add a user-defined worker process. This function is usually used to create a special worker process for monitoring, reporting or other special tasks. For specific content, please refer to Swoole official website addProcess
Things to note:
$server
object, such as getClientList/getClientInfo/stats
Worker/Task
process, you can call the method provided by $process
to communicate with the child process.$server->sendMessage
to communicate with the Worker/Task
process.Server->task/taskwait
interface cannot be used in the user process.Server->send/close
while(true)
(as shown in the example below) or EventLoop loop (such as creating a timer), otherwise the user process will continue to exit and restart.Usage example
Add a user-defined worker process and add process
configuration in ./config/conf.php
as follows:
'process' => [
[AppControllerProcess::class, 'addProcess']
],
Controller example:
<?php
namespace AppController;
class Process
{
//添加用户自定义的工作进程
public function addProcess($server)
{
return new SwooleProcess(function ($process) use ($server) {
while (true) {
Co::sleep(1);
echo "Hello, api-swoole!rn";
}
}, false, 2, 1);
}
}
The framework has by default registered five customized events: workerStart、open、close、task、finish
. Processing business code can be added to the corresponding event callback according to specific business characteristics. ./confg/events.php
, each event allows multiple callbacks
<?php
return [
'workerStart' => [
[AppExtPool::class, 'startPool'],//启动连接池
// [AppControllerEventsDemo::class, 'workerStart'],
],
'open' => [
[AppControllerEventsDemo::class, 'open'],
],
'close' => [
[AppControllerEventsDemo::class, 'close'],
],
'task' => [
[AppControllerEventsDemo::class, 'task'],
],
'finish' => [
[AppControllerEventsDemo::class, 'finish'],
],
];