🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# thinkphp5根目录下安装 官方文档 https://www.kancloud.cn/manual/thinkphp5_1/354134 ~~~ composer require topthink/think-worker=2.0.* ~~~ ## 找到以下路径: ## worker_server.php为配置文件 ``` tp5\config\worker_server.php ``` ~~~ <?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- use think\facade\Env; // +---------------------------------------------------------------------- // | Workerman设置 仅对 php think worker:server 指令有效 // +---------------------------------------------------------------------- return [ // 扩展自身需要的配置 'protocol' => 'websocket', // 协议 支持 tcp udp unix http websocket text 'host' => '0.0.0.0', // 监听地址 'port' => 2345, // 监听端口 'socket' => '', // 完整监听地址 'context' => [], // socket 上下文选项 'worker_class' => '', // 自定义Workerman服务类名 支持数组定义多个服务 // 支持workerman的所有配置参数 'name' => 'thinkphp', 'count' => 4, 'daemonize' => false, 'pidFile' => Env::get('runtime_path') . 'worker.pid', // 支持事件回调 // onWorkerStart 'onWorkerStart' => function ($worker) { }, // onWorkerReload 'onWorkerReload' => function ($worker) { }, // onConnect 'onConnect' => function ($connection) { }, // onMessage 'onMessage' => function ($connection, $data) { $connection->send('receive success'); }, // onClose 'onClose' => function ($connection) { }, // onError 'onError' => function ($connection, $code, $msg) { echo "error [ $code ] $msg\n"; }, ]; ~~~ # 进行配置 ## worker_class为多个服务的时候用到 ## 里面的值为`模块/命名空间/类名` ~~~ 'worker_class' => 'app\workerman\worker', // 自定义Workerman服务类名 支持数组定义多个服务 ~~~ ## 代码案例: ~~~ <?php namespace app\workerman; use think\worker\Server; class worker extends Server { protected $socket = 'text://0.0.0.0:1111'; protected $option=['name'=>"学习",'count'=>8]; //修改名称,启动多少进程等配置 public function onWorkerStart(){ } } ~~~ # 支持以下函数 ![](https://img.kancloud.cn/51/b6/51b6920b30bdb9648872a7f21d1ea474_1492x1580.png) # 修改名称等配置 ~~~ protected $option=['name'=>"学习",'count'=>8]; ~~~ ![](https://img.kancloud.cn/58/4f/584fba8c1077cd69545f8b111845b881_2478x455.png)