目录

1.8.2.4 protocol

优质
小牛编辑
135浏览
2023-12-01

要求(workerman >= 3.2.7)

说明:

string Worker::$protocol

设置当前Worker实例的协议类。

注:协议处理类可以直接在初始化Worker在监听参数时直接指定。例如

$worker = new Worker('http://0.0.0.0:8686');

范例

use WorkermanWorker;
require_once __DIR__ . '/Workerman/Autoloader.php';

$worker = new Worker('tcp://0.0.0.0:8686');
$worker->protocol = 'Workerman\Protocols\Http';

$worker->onMessage = function($connection, $data)
{
    var_dump($_GET, $_POST);
    $connection->send("hello");
};

// 运行worker
Worker::runAll();

以上代码等价于下面代码

use WorkermanWorker;
require_once __DIR__ . '/Workerman/Autoloader.php';

/**
 * 首先会查看用户是否有自定义ProtocolsHttp协议类,
 * 如果没有使用workerman内置协议类WorkermanProtocolsHttp
 */
$worker = new Worker('http://0.0.0.0:8686');

$worker->onMessage = function($connection, $data)
{
    var_dump($_GET, $_POST);
    $connection->send("hello");
};

// 运行worker
Worker::runAll();