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();