多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Http\\Server->on [TOC] 注册事件回调函数,与`Server->on`相同,不同之处是: * `Http\Server->on`不接受`onConnect`/`onReceive`回调设置 * `Http\Server->on`额外接受1种新的事件类型`onRequest` ## onRequest事件 ~~~ $http_server->on('request', function(swoole_http_request $request, swoole_http_response $response) { $response->end("<h1>hello swoole</h1>"); }) ~~~ 在收到一个完整的`Http`请求后,会回调此函数。回调函数共有`2`个参数: * `$request`,`Http`请求信息对象,包含了`header/get/post/cookie`等相关信息 * `$response`,`Http`响应对象,支持`cookie/header/status`等`Http`操作 * 在`onRequest`回调函数返回时底层会销毁`$request`和`$response`对象,如果未执行`$response->end()`操作,底层会自动执行一次`$response->end("")` > `onRequest`在`1.7.7`或更高版本可用 > `$response/$request`对象传递给其他函数时,不要加`&`引用符号 > `$response/$request`对象传递给其他函数后,引用计数会增加,`onRequest`退出时不会销毁