ThinkPHP 5.1 Swoole 扩展
===============
## 安装
首先按照Swoole官网说明安装swoole扩展,然后使用
~~~
composer require topthink/think-swoole
~~~
安装swoole扩展。
## 使用方法
### HttpServer
直接在命令行下启动服务端。
~~~
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]
~~~
### Server
可以支持直接启动一个Swoole server
~~~
php think swoole:server
~~~
会在0.0.0.0:9508启动一个Websocket服务。
如果需要自定义参数,可以在config/swoole_server.php中进行配置,包括:
配置参数 | 描述
--- | ---
type| 服务类型
host | 监听地址
port | 监听端口
mode | 运行模式
sock_type | Socket type
并且支持swoole所有的参数。
也支持使用闭包方式定义相关事件回调。
~~~
return [
// 扩展自身配置
'host' => '0.0.0.0', // 监听地址
'port' => 9501, // 监听端口
'type' => 'socket', // 服务类型 支持 socket http server
'mode' => SWOOLE_PROCESS,
'sock_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 $serverType = 'socket';
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
~~~
### 配置信息详解
swoole.php
```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;
// +----------------------------------------------------------------------
// | Swoole设置 php think swoole命令行下有效
// +----------------------------------------------------------------------
return [
// 扩展自身配置
'host' => '0.0.0.0', // 监听地址
'port' => 9501, // 监听端口
'mode' => '', // 运行模式 默认为SWOOLE_PROCESS
'sock_type' => '', // sock type 默认为SWOOLE_SOCK_TCP
'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',//swoole主进程pid存放文件
'log_file' => Env::get('runtime_path') . 'swoole.log',//swoole日志存放文件
'document_root' => Env::get('root_path') . 'public',//设置静态服务根目录
'enable_static_handler' => true,//是否由SWOOLE底层自动处理静态文件,TRUE表示SWOOLE判断是否存在静态文件,如果存在则直接返回静态文件信息
'timer' => true,//是否开启系统定时器
'interval' => 500,//系统定时器 时间间隔
'task_worker_num' => 1,//swoole 任务工作进程数量
'user' =>'www',//表示swoole worker进程所属的管理员名称,如果要绑定1024以下端口则必须要求具有root权限,如果设置了该项,则除主进程外的所有进程都运行于指定用户下
];
```
timer.php
```php
<?php
/**
* Created by PhpStorm.
* User: xavier
* Date: 2018/8/15
* Time: 下午2:14
* 秒 分 时 日 月 星期几
* crontab 格式 * * * * * * => "类"
* *中间一个空格
* 系统定时任务需要在swoole.php中开启
* 自定义定时器不受其影响
*/
return [
'*/5 * * * * *' => '\\app\\lib\\Timer',//时间配置方式参考crontab,主要是增加秒,其他和crontab一致,对应Value为定时器接口实现类的完整命名空间(包含类名)
];
```
### 异步任务投递
1.异步任务接口实现
```php
<?php
/**
* Created by PhpStorm.
* User: xavier
* Date: 2018/8/19
* Time: 下午8:28
*/
namespace app\lib;
use think\swoole\template\Task;
class TestTask extends Task
{
public function _initialize(...$arg)
{
// TODO: Implement _initialize() method.
}
public function run($serv, $task_id, $fromWorkerId)
{
// TODO: Implement run() method.
}
}
```
2.异步任务投递在控制器中的使用
```php
<?php
namespace app\index\controller;
use think\swoole\facade\Task;
class Index
{
public function hello()
{
//投递任务模板
$task=new \app\lib\TestTask();
Task::async($task);
//异步任务投递闭包
Task::async(function ($serv, $task_id, $data) {
$i = 0;
while ($i < 10) {
$i++;
echo $i;
sleep(1);
}
});
return 'hello' ;
}
}
```
### 定时器的使用
定时器分为系统定时器和自定义定时器,系统定时器需要在配置文件(timer.php)中进行配置,会根据配置由系统自动处理
1. 定时器接口的实现
```php
<?php
namespace app\lib;
/**
* Created by PhpStorm.
* User: xavier
* Date: 2018/8/19
* Time: 下午8:01
*/
use think\swoole\template\Timer;
class TestTimer extends Timer
{
public function _initialize(...$arg)
{
// TODO: Implement _initialize() method.
}
public function run()
{
$i=0;
// TODO: Implement run() method.
while($i<10){
var_dump(12);
$i++;
sleep(1);
}
}
}
```
2. 系统定时器的使用
参考上面timer.php的配
没有合适的资源?快使用搜索试试~ 我知道了~
基于PHP的Timely在线客服系统开源版.zip
共1180个文件
php:638个
test:191个
gif:75个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 91 浏览量
2023-07-23
00:23:50
上传
评论
收藏 8.18MB ZIP 举报
温馨提示
基于PHP的Timely在线客服系统开源版.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于PHP的Timely在线客服系统开源版.zip (1180个子文件)
soho.min.css 467KB
layui.css 71KB
font-awesome.css 37KB
font-awesome.min.css 30KB
install.css 19KB
wangEditor.css 17KB
wangEditor.min.css 15KB
layer.css 14KB
layuimini.css 11KB
layui.mobile.css 10KB
zyupload-1.0.0.min.css 9KB
laydate.css 7KB
perfect-scrollbar.min.css 2KB
step.css 1KB
code.css 1KB
public.css 676B
treetable.css 294B
ip2region.db 6.74MB
phpunit.xml.dist 1KB
phpunit.xml.dist 626B
fontawesome-webfont.eot 162KB
iconfont.eot 41KB
hiddeninput.exe 9KB
inlineHTMLandPHPtest.file-test 433B
haltCompiler.file-test 219B
onlyInlineHTML.file-test 150B
onlyPHP.file-test 140B
59.gif 10KB
22.gif 10KB
24.gif 8KB
13.gif 7KB
16.gif 7KB
39.gif 6KB
64.gif 6KB
63.gif 6KB
50.gif 6KB
loading-0.gif 6KB
4.gif 6KB
1.gif 5KB
42.gif 5KB
71.gif 5KB
21.gif 5KB
20.gif 5KB
29.gif 5KB
70.gif 4KB
5.gif 4KB
17.gif 4KB
27.gif 4KB
9.gif 4KB
44.gif 4KB
11.gif 4KB
8.gif 4KB
3.gif 4KB
23.gif 4KB
34.gif 4KB
41.gif 4KB
38.gif 4KB
65.gif 3KB
32.gif 3KB
45.gif 3KB
7.gif 3KB
12.gif 3KB
26.gif 3KB
60.gif 3KB
2.gif 3KB
40.gif 3KB
25.gif 3KB
19.gif 3KB
66.gif 3KB
18.gif 3KB
46.gif 3KB
10.gif 3KB
28.gif 3KB
51.gif 3KB
57.gif 3KB
67.gif 3KB
0.gif 3KB
48.gif 3KB
43.gif 3KB
30.gif 2KB
61.gif 2KB
33.gif 2KB
69.gif 2KB
14.gif 2KB
47.gif 2KB
36.gif 2KB
49.gif 2KB
58.gif 2KB
6.gif 2KB
54.gif 2KB
53.gif 2KB
56.gif 2KB
62.gif 2KB
31.gif 2KB
55.gif 2KB
35.gif 2KB
15.gif 2KB
loading-2.gif 2KB
37.gif 1KB
68.gif 1KB
共 1180 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
助力毕业
- 粉丝: 2179
- 资源: 5136
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 农村小别墅图纸编号D040-三层-08.30&14.60米-施工图.dwg
- 三层别墅图纸编号D039-三层-16.70&14.70米- 结构图.dwg
- 三层农村小别墅编号D038-三层-11.90&13.50米-施工图.dwg
- 采购组·主管业务原型集与说明(Xmind书写)
- 三层图纸编号D037-三层-10.40&15.10米-施工图.dwg
- 三层别墅图纸编号D036-三层-14.04&15.44米-施工图.dwg
- 三层别墅图纸编号D035-三层-09.30&13.80米- 施工图.dwg
- 三层别墅图纸编号D032-三层-07.20&10.00米- 施工图.dwg
- 三层别墅图纸编号D031-三层-08.00&09.70米- 施工图.dwg
- aaaaaaaaaaa
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功