<?php
class Http
{
const HOST = '0.0.0.0'; // 监听的IP地址, 0.0.0.0 表示监听所有IP地址
const PROT = '9503';
public $http = null; // 存放http连接资源
public $config = [
// 进程数
'worker_num' => 8,
// 开启静态文件请求
'enable_static_handler' => true,
// 配置静态文件根目录 (可以在浏览器访问)
'document_root' => '/www/wwwroot/swoole_gyy/data',
];
public function __construct()
{
// 创建 http server对象
$this->http = new Swoole\Http\Server(self::HOST, self::PROT);
// 设置参数
$this->http->set($this->config);
// 监听客户端的请求响应
$this->http->on('request', [$this, 'onRequest']);
// 启动服务
$this->http->start();
}
/**
* 监听客户端的请求响应
* $request 请求相关信息 如 GET/POST
* $response 用来操作request的响应
*/
public function onRequest($request, $response)
{
// 设置cookie
$response->cookie('name', 'huanhuan', time()+ 100);
// 设置header
$response->header("Content-Type", "text/html; charset=utf-8");
// 发送消息到客户端 (将url参数输出到页面中)
$response->end('hello - '. json_encode($request->get));
}
}
new Http();
swoole HTTP服务器
最新推荐文章于 2024-03-29 11:31:42 发布