tp6中workerman的基础使用
所属分类:PHP之旅 | 最后更新于:2021-01-08 17:21:25 | 阅读次数:488

composer 安装

composer require topthink/think-worker

启用http服务

php think worker

Starting Workerman http server... You can exit with `CTRL-C` ----------------------- WORKERMAN ----------------------------- Workerman version:3.5.31 PHP version:7.3.4 ------------------------ WORKERS ------------------------------- worker listen processes status none http://0.0.0.0:2346 1 [ok]

启用socket服务

php think worker:server

Starting Workerman server... ----------------------- WORKERMAN ----------------------------- Workerman version:3.5.31 PHP version:7.3.4 ------------------------ WORKERS ------------------------------- worker listen processes status none websocket://0.0.0.0:2345 1 [ok]

修改配置文件

安装成功后会自动在config文件夹下创建worker_server.php文件,如果没有请手动创建

<?php /** * * User: cfn * Date: 2021/1/8 * Email: <cfn@leapy.cn> */ return [ // 自定义Workerman服务类名 支持数组定义多个服务 'worker_class' => [ 'kernel\service\socket' ], ];

在kernel\service目录下新建文件socket.php

<?php /** * * User: cfn * Date: 2021/1/8 * Email: <cfn@leapy.cn> */ namespace kernel\service; use think\worker\Server; use Workerman\Worker; class socket extends Server { /** * 协议 * @var string */ protected $protocol = "websocket"; /** * 监听地址 * @var string */ protected $host = '0.0.0.0'; /** * 端口 * @var string */ protected $port = 5500; /** * 基础配置 * @var array */ protected $option = [ 'count' => 1, 'name' => 'admin' ]; /** * 定时程序 * @var null */ protected $time; /** * @var Worker */ protected $worker; /** * worker constructor. * @param Worker|null $worker */ protected function init(Worker $worker = null) { parent::init(); $this->worker = $worker; } }

再次运行命令

php think worker:server

就会发现变化

Starting Workerman server... ----------------------- WORKERMAN ----------------------------- Workerman version:3.5.31 PHP version:7.3.4 ------------------------ WORKERS ------------------------------- worker listen processes status admin websocket://0.0.0.0:5500 1 [ok]
文章作者:cfn
本文链接:
500字剩余