您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman的Http服务-请求-获得请求对象
发布时间:2021-12-04 23:56:34编辑:雪饮阅读()
获得请求对象
请求对象一律在onMessage回调函数中获取,框架会自动将Request对象通过回调函数第二个参数传递进来。
实例:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8484');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
// $request为请求对象,这里没有对请求对象执行任何操作直接返回hello给浏览器
var_dump($request);
$connection->send("hello");
};
// 运行worker
Worker::runAll();
?>
实例运行并有客户端请求时:
[root@izj6c2jeancylo0ppo4vz5z workerman]# php test.php start
Workerman[test.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.22 PHP version:7.0.33
------------------------------------------ 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.
object(Workerman\Protocols\Http\Request)#7 (5) {
["connection"]=>
object(Workerman\Connection\TcpConnection)#4 (23) {
["onMessage"]=>
object(Closure)#2 (1) {
["parameter"]=>
array(2) {
["$connection"]=>
string(10) "<required>"
["$request"]=>
string(10) "<required>"
}
}
["onClose"]=>
NULL
["onError"]=>
NULL
["onBufferFull"]=>
NULL
["onBufferDrain"]=>
NULL
["protocol"]=>
string(24) "Workerman\Protocols\Http"
["transport"]=>
string(3) "tcp"
["worker"]=>
object(Workerman\Worker)#3 (29) {
["id"]=>
int(0)
["name"]=>
string(4) "none"
["count"]=>
int(1)
["user"]=>
string(4) "root"
["group"]=>
string(0) ""
["reloadable"]=>
bool(true)
["reusePort"]=>
bool(false)
["onWorkerStart"]=>
NULL
["onConnect"]=>
NULL
["onMessage"]=>
object(Closure)#2 (1) {
["parameter"]=>
array(2) {
["$connection"]=>
string(10) "<required>"
["$request"]=>
string(10) "<required>"
}
}
["onClose"]=>
NULL
["onError"]=>
NULL
["onBufferFull"]=>
NULL
["onBufferDrain"]=>
NULL
["onWorkerStop"]=>
NULL
["onWorkerReload"]=>
NULL
["transport"]=>
string(3) "tcp"
["connections"]=>
array(1) {
[1]=>
*RECURSION*
}
["protocol"]=>
string(24) "Workerman\Protocols\Http"
["_autoloadRootPath":protected]=>
string(10) "/workerman"
["_pauseAccept":protected]=>
bool(false)
["stopping"]=>
bool(false)
["_mainSocket":protected]=>
resource(18) of type (stream)
["_socketName":protected]=>
string(19) "http://0.0.0.0:8484"
["_localSocket":protected]=>
NULL
["_context":protected]=>
resource(12) of type (stream-context)
["workerId"]=>
string(32) "000000001af426a40000000004598309"
["socket"]=>
string(19) "http://0.0.0.0:8484"
["status"]=>
string(13) "<g> [OK] </g>"
}
["bytesRead"]=>
int(148)
["bytesWritten"]=>
int(0)
["id"]=>
int(1)
["_id":protected]=>
int(1)
["maxSendBufferSize"]=>
int(1048576)
["maxPackageSize"]=>
int(10485760)
["_socket":protected]=>
resource(23) of type (stream)
["_sendBuffer":protected]=>
string(0) ""
["_recvBuffer":protected]=>
string(0) ""
["_currentPackageLength":protected]=>
int(0)
["_status":protected]=>
int(2)
["_remoteAddress":protected]=>
string(15) "127.0.0.1:55948"
["_isPaused":protected]=>
bool(false)
["_sslHandshakeCompleted":protected]=>
bool(false)
["__request"]=>
*RECURSION*
}
["session"]=>
NULL
["properties"]=>
array(0) {
}
["_buffer":protected]=>
string(148) "GET / HTTP/1.1
Host: 127.0.0.1:8484
User-Agent: ELinks/0.12pre6 (textmode; Linux; -)
Accept: */*
Accept-Language: en
Connection: Keep-Alive
"
["_data":protected]=>
NULL
}
客户端请求:
[root@izj6c2jeancylo0ppo4vz5z ~]# elinks 127.0.0.1:8484 --dump
hello
关键字词:workerman,Http,请求
相关文章
- workerman的Http服务-请求-获得请求参数post
- workerman的Http服务-请求-获得请求参数get
- workerman的AsyncUdpConnection类的send方法
- workerman的AsyncUdpConnection类的__construct构造方
- workerman的AsyncTcpConnection类的transport属性都利
- workerman的AsyncTcpConnection类的reConnect 方法实
- workerman的AsyncTcpConnection类的connect 方法实现
- workerman类TcpConnection的接口send
- workerman类TcpConnection的接口pipe通道实现流量控制
- workerman类TcpConnection的接口pauseRecv实现上传流