protocol
要求(workerman >= 3.2.7)
说明:
string Worker::$protocol
设置当前Worker实例的协议类。
注:协议处理类可以直接在初始化Worker在监听参数时直接指定。例如
$worker = new Worker('http://0.0.0.0:8686');
范例
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('tcp://0.0.0.0:8686');
$worker->protocol = 'Workerman\\Protocols\\Http';
$worker->onMessage = function(TcpConnection $connection, $data)
{
var_dump($_GET, $_POST);
$connection->send("hello");
};
// 运行worker
Worker::runAll();
以上代码等价于下面代码
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
/**
* 首先会查看用户是否有自定义\Protocols\Http协议类,
* 如果没有使用workerman内置协议类Workerman\Protocols\Http
*/
$worker = new Worker('http://0.0.0.0:8686');
$worker->onMessage = function(TcpConnection $connection, $data)
{
var_dump($_GET, $_POST);
$connection->send("hello");
};
// 运行worker
Worker::runAll();