transport属性

要求(workerman >= 3.3.4)

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

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

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

示例 (https连接)

  1. use \Workerman\Worker;
  2. use \Workerman\Connection\AsyncTcpConnection;
  3. require_once __DIR__ . '/Workerman/Autoloader.php';
  4. $task = new Worker();
  5. // 进程启动时异步建立一个到www.baidu.com连接对象,并发送数据获取数据
  6. $task->onWorkerStart = function($task)
  7. {
  8. $connection_to_baidu = new AsyncTcpConnection('tcp://www.baidu.com:443');
  9. // 设置为ssl加密连接
  10. $connection_to_baidu->transport = 'ssl';
  11. $connection_to_baidu->onConnect = function($connection_to_baidu)
  12. {
  13. echo "connect success\n";
  14. $connection_to_baidu->send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: keep-alive\r\n\r\n");
  15. };
  16. $connection_to_baidu->onMessage = function($connection_to_baidu, $http_buffer)
  17. {
  18. echo $http_buffer;
  19. };
  20. $connection_to_baidu->onClose = function($connection_to_baidu)
  21. {
  22. echo "connection closed\n";
  23. };
  24. $connection_to_baidu->onError = function($connection_to_baidu, $code, $msg)
  25. {
  26. echo "Error code:$code msg:$msg\n";
  27. };
  28. $connection_to_baidu->connect();
  29. };
  30. // 运行worker
  31. Worker::runAll();