1.10.4 transport

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

要求(workerman >= 3.3.4)

设置传输属性,可选值为 tcpssl,默认是tcp。

transport为 ssl 时,要求PHP必须安装openssl扩展

当把Workerman作为客户端向服务端发起ssl加密连接(https连接、wss连接等)时请设置此选项为ssl,例如下面的例子。

示例 (https连接)

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

$task = new Worker();
// 进程启动时异步建立一个到www.baidu.com连接对象,并发送数据获取数据
$task->onWorkerStart = function($task)
{
    $connection_to_baidu = new AsyncTcpConnection('tcp://www.baidu.com:443');

    // 设置为ssl加密连接
    $connection_to_baidu->transport = 'ssl';

    $connection_to_baidu->onConnect = function($connection_to_baidu)
    {
        echo "connect successn";
        $connection_to_baidu->send("GET / HTTP/1.1rnHost: www.baidu.comrnConnection: keep-alivernrn");
    };
    $connection_to_baidu->onMessage = function($connection_to_baidu, $http_buffer)
    {
        echo $http_buffer;
    };
    $connection_to_baidu->onClose = function($connection_to_baidu)
    {
        echo "connection closedn";
    };
    $connection_to_baidu->onError = function($connection_to_baidu, $code, $msg)
    {
        echo "Error code:$code msg:$msgn";
    };
    $connection_to_baidu->connect();
};

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