swoole HTTP服务器

<?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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值