Thinkphp中应用Swoole
Swoole是一个面向消费环境的 PHP 异步网络通讯引擎。使 PHP 开发职员能够编写高功能的异步并发 TCP、UDP、Unix Socket、HTTP,WebSocket 效劳。
装置
起首依照Swoole官网阐明装置swoole扩大,而后装置think-swoole扩大。
composer require topthink/think-swoole=2.0.*应用
应用
Swoole今朝没有支持Windows
应用Swoole作为HttpServer
饬令行下启动效劳端(需求2.0+版本think-swoole扩大)
间接正在饬令行下启动效劳端。
php think swoole
启动实现后,会正在0.0.0.0:9501启动一个HTTP Server,能够间接拜访以后的使用。
swoole的参数能够正在使用设置装备摆设目次下的swoole.php外面设置装备摆设(详细参考设置装备摆设文件内容)。
假如需求应用守护过程形式运转,能够应用
php think swoole -d
或许正在swoole.php文件中设置
'daemonize' => true
留意:因为onWorkerStart运转的时分不HTTP_HOST,因而最佳正在使用设置装备摆设文件中设置app_host
支持的操作包罗
php think swoole [start|stop|reload|restart]
因为onWorkerStart运转的时分不HTTP_HOST,因而最佳正在使用设置装备摆设文件中设置app_host参数
启动后,能够应用
http://127.0.0.1:9501
拜访你的使用。
假如需求设置装备摆设地点以及端口,能够正在使用设置装备摆设目次下添加
swoole.php设置装备摆设文件,而后设置:
<?phpreturn [ 'host' => 'tp5.com', 'port' => 9508,]; 能够支持Swoole本身的设置装备摆设参数设置,例如: <?phpreturn [ 'host' => 'tp5.com', 'port' => 9508, 'worker_num' => 4, 'max_request' => 1000,];
扩大中界说了onWorkerStart以及onRequest事情回调办法(假如没有相熟请没有要随便交换),假如你需求自界说swoole的事情回调办法,能够正在设置装备摆设文件中应用闭包界说。
<?phpreturn [ 'host' => 'tp5.com', 'port' => 9508, 'worker_num' => 4, 'max_request' => 1000, 'WorkerStop' => function($server, $worker_id){ // 增加你的代码 },];
或许间接正在设置装备摆设文件中增加
应用Swoole作为Server效劳端
能够支持间接启动一个Swoole server(需求2.0.9+版本)
php think swoole:server
会正在0.0.0.0:9508启动一个Websocket效劳。
假如需求自界说参数,能够正在config/swoole_server.php中进行设置装备摆设,包罗:
设置装备摆设参数 | 形容 |
type | 效劳类型 |
host | 监听地点 |
port | 监听端口 |
mode | 运转模式 |
socket | Socket type |
而且支持swoole一切的参数。
也支持应用闭包形式界说相干事情回调。
return [ // 扩大本身设置装备摆设 'host' => '0.0.0.0', // 监听地点 'port' => 9501, // 监听端口 'type' => 'socket', // 效劳类型 支持 socket http server 'mode' => SWOOLE_PROCESS, 'socket_type' => SWOOLE_SOCK_TCP, // 能够支持swoole的一切设置装备摆设参数 'daemonize' => false, // 事情回调界说 'onOpen' => function ($server, $request) { echo "server: handshake success with fd{$request->fd}\n"; }, 'onMessage' => function ($server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $server->push($frame->fd, "this is server"); }, 'onRequest' => function ($request, $response) { $response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>"); }, 'onClose' => function ($ser, $fd) { echo "client {$fd} closed\n"; },];
也能够应用自界说的效劳类
<?php namespace app\http;use think\swoole\Server;class Swoole extends Server{ protected $host = '127.0.0.1'; protected $port = 9502; protected $option = [ 'worker_num'=> 4, 'daemonize' => true, 'backlog' => 128 ]; public function onReceive($server, $fd, $from_id, $data) { $server->send($fd, 'Swoole: '.$data); }}
支持swoole一切的回调办法界说(回调办法必需是public类型)
serverType 属性界说为 socket或许http 则支持swoole的swoole_websocket_server以及swoole_http_server
而后正在swoole_server.php中添加设置装备摆设参数:
return [ 'swoole_class' => 'app\http\Swoole',];
界说该参数后,其它设置装备摆设参数均再也不无效。
正在饬令行启动效劳端
php think swoole:server
支持reload|restart|stop|status 操作
php think swoole:server reload
保举教程:《PHP视频教程》
以上就是直击Thinkphp中的Swoole的具体内容,更多请存眷资源魔其它相干文章!
标签: php开发教程 php开发资料 php开发自学 Swoole
抱歉,评论功能暂时关闭!