您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman实现http客户端及chunk服务端
发布时间:2021-12-12 14:40:51编辑:雪饮阅读()
为了实现客户端我们需要在workerman源码基础上用composer新增下http客户端的子源码包。
[root@localhost workerman]# /usr/local/php734/bin/php -c /usr/local/php734/lib/php/php.ini /usr/local/bin/composer require workerman/http-client
有了http客户端的源码包后那么结合chunk的一个实例如:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Chunk;
use Workerman\Protocols\Http\Response;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8484');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
// 首先发送一个带Transfer-Encoding: chunked头的Response响应
$connection->send(new Response(200, array('Transfer-Encoding' => 'chunked'), 'hello'));
// 后续Chunk数据用Workerman\Protocols\Http\Chunk类发送
for($i=1;$i<=10000;$i++){
$url="http://www.baidu.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
//永不超时
curl_setopt($ch, CURLOPT_TIMEOUT,0);
$sContent = curl_exec($ch);
$connection->send(new Chunk('第'.$i.'段数据'.$sContent.'<br/>'));
}
};
// 运行worker
Worker::runAll();
?>
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Chunk;
use Workerman\Protocols\Http\Response;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8484');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
// 首先发送一个带Transfer-Encoding: chunked头的Response响应
$connection->send(new Response(200, array('Transfer-Encoding' => 'chunked'), 'hello'));
// 后续Chunk数据用Workerman\Protocols\Http\Chunk类发送
for($i=1;$i<=10000;$i++){
$url="http://www.baidu.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
//永不超时
curl_setopt($ch, CURLOPT_TIMEOUT,0);
$sContent = curl_exec($ch);
$connection->send(new Chunk('第'.$i.'段数据'.$sContent.'<br/>'));
}
};
// 运行worker
Worker::runAll();
?>
这里有用到curl,需要为你的php安装curl扩展,安装扩展过程中有可能会出现curl版本号不匹配类似提示,可以尝试下
[root@localhost curl-7.80.0]# yum -y install curl-devel
有时候你看到你的版本号(curl --version)貌似是满足需求的,但是仍旧提示类似版本号不同的错误,则可能是说devel后缀的包。
扩展包安装好之后服务端实例运行:
[root@localhost workerman]# /usr/local/php734/bin/php -c /usr/local/php734/lib/php/php.ini start.php start
Workerman[start.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.22 PHP version:7.3.4
------------------------------------------ WORKERS ------------------------------------------
proto user worker listen processes status
tcp root none http://0.0.0.0:8484 1 [OK]
---------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
然后客户端请求如:
关键字词:workerman,http,chunk,客户端,服务端
相关文章
- workerman的http服务-基本调试 - 查看运行状态
- workerman的http服务-基本调试
- workerman的http服务-SSE(推送服务,服务端主推)
- workerman的http服务-session管理-更改存储驱动
- workerman的http服务-session管理-设置session存储位
- workerman的http服务-session管理-更改session存储引
- workerman的http服务-session会话-判断对应session数
- workerman的http服务-session会话-删除所有session数
- workerman的http服务-session会话-获取并删除session
- workerman的http服务-session会话-删除session数据