栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

SWOOLE 从入门到放弃之写个小框架(六)

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SWOOLE 从入门到放弃之写个小框架(六)

好,能跑起来就说明两个问题,
一,加载器成功了,
二,我没蒙错。

接下来就开始来点稍干的了,先看代码

    public function onRequest($request,$response){
 $response->end("hello");
    }

这个 onRequest 是HTTP服务的核心,没有它,就没法接收来自客户端的请求,也就没法给客户端返回正确的内容,最重要的是如果没有它,HTTP服务跑不起来。
这个回调包含两个参数 $request和$response ,如字面意思 $request就是HTTP请求对像接收到的客户端请求的相关信息,包含GET,POST,cookie,HEADER等,看文档。$response就是服务端给客户端回应时对象,看文档。

接下来就来处理它。

首先,我们要有一个总调度,它主要实现的功能就是接收这两个参数,然后路由分析路径,并根据返回的路由信息加载对应的项目文件。看到了吗,加载,这两个字终于用上了。
创建文件 frame/Lib/App.php,贴代码

end(var_export ($response,TRUE));
    }
}

修改frame/Lib/Server.php,再贴

server = new swoole_http_server('0.0.0.0',9501);
 $this->server->on('start', [$this, 'onStart']);
 $this->server->on('request' ,[$this,'onRequest']);
 $this->server->start();
    }

    public function onStart($server){
 echo "服务启动  http://0.0.0.0:9501",PHP_EOL;
    }

    public function onRequest($request,$response){
 App::get_instance()->http ($request,$response);
    }

    public function onClose($server,$fd){
    }
}

运行启动文件 /start.php,看控制台

root@localhost:/piz$ php start.php 
服务启动  http://0.0.0.0:9501
SwooleHttpRequest Object
(
    [fd] => 1
    [header] => Array
 (
     [host] => 192.168.1.111:9501
     [connection] => keep-alive
     [upgrade-insecure-requests] => 1
     [user-agent] => Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
     [accept] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
     [accept-encoding] => gzip, deflate
     [accept-language] => zh-CN,zh;q=0.9
 )

    [server] => Array
 (
     [request_method] => GET
     [request_uri] => /
     [path_info] => /
     [request_time] => 1523500869
     [request_time_float] => 1523500869.6587
     [server_port] => 9501
     [remote_port] => 2751
     [remote_addr] => 192.168.1.100
     [master_time] => 1523500869
     [server_protocol] => HTTP/1.1
     [server_software] => swoole-http-server
 )

    [request] => 
    [cookie] => 
    [get] => 
    [files] => 
    [post] => 
    [tmpfiles] => 
)

看浏览器

OK,漂亮。

小伙伴们不要在意我的host,我是在WIN10上开发,VBOX上运行。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/228928.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号