1. 下载 tp 框架, 版本 5.1.35 , 下载 think-swoole, 版本 2.0.17
2. 在 config 下书写 swoole.PHP
- <?PHP
- use think\facade\Env;
- // +----------------------------------------------------------------------
- // | Swoole 设置 PHP think swoole 命令行下有效
- // +----------------------------------------------------------------------
- return [
- // 扩展自身配置
- 'host' => '0.0.0.0', // 监听地址
- 'port' => 9501, // 监听端口
- 'worker_num' => 4,
- 'max_request' => 1000,
- 'app_path' => '/web/twzb/application/',
- 'cache_size'=>1024*8,
- /* 'mode' => '', // 运行模式 默认为 SWOOLE_PROCESS
- 'sock_type' => '', // sock type 默认为 SWOOLE_SOCK_TCP
- 'server_type' => 'http', // 服务类型 支持 http websocket
- 'app_path' => '', // 应用地址 如果开启了'daemonize'=>true 必须设置 (使用绝对路径)
- 'file_monitor' => false, // 是否开启 PHP 文件更改监控 (调试模式下自动开启)
- 'file_monitor_interval' => 2, // 文件变化监控检测时间间隔 (秒)
- 'file_monitor_path' => [], // 文件监控目录 默认监控 application 和 config 目录
- // 可以支持 swoole 的所有配置参数
- 'pid_file' => Env::get('runtime_path') . 'swoole.pid',
- 'log_file' => Env::get('runtime_path') . 'swoole.log',
- 'document_root' => Env::get('root_path') . 'public',
- 'enable_static_handler' => true,
- 'timer' => true,// 是否开启系统定时器
- 'interval' => 500,// 系统定时器 时间间隔
- 'task_worker_num' => 1,//swoole 任务工作进程数量 */
- ];
2. 在 config 下书写 swoole_server.PHP
- <?PHP
- use think\facade\Env;
- // +----------------------------------------------------------------------
- // | Swoole 设置 PHP think swoole:server 命令行下有效
- // +----------------------------------------------------------------------
- return [
- 'swoole_class' => 'app\http\Swoole', // 自定义服务类名称
- ];
3. 书写 Swoole.PHP
- <?PHP
- namespace App\http;
- use think\swoole\Server;
- class Swoole extends Server
- {
- protected $host = '0.0.0.0';
- protected $port = 9502;
- protected $option = [
- 'worker_num'=> 4,
- 'daemonize' => true,
- 'backlog' => 128,
- 'app_path' => '/web/twzb/application/',
- 'cache_size'=>1024*8,
- ];
- public function onReceive($server, $fd, $from_id, $data)
- {
- $server->send($fd, 'Swoole:'.$data);
- }
- public function onRequest($request, $response)
- {
- $server->send('Swoole:');
- }
- }
使用命令
PHP think swoole
使用命令
PHP think swoole:server
来源: http://www.bubuko.com/infodetail-3498750.html