当前位置: 首页 > 文档资料 > WorkerMan 3.x 手册 >

AsyncTcpConnection类 - transport属性

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

要求(workerman >= 3.3.4)

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

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

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

示例 (https连接)

  1. use WorkermanWorker;
  2. use WorkermanConnectionAsyncTcpConnection;
  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 successn";
  14. $connection_to_baidu->send("GET / HTTP/1.1rnHost: www.baidu.comrnConnection: keep-alivernrn");
  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 closedn";
  23. };
  24. $connection_to_baidu->onError = function($connection_to_baidu, $code, $msg)
  25. {
  26. echo "Error code:$code msg:$msgn";
  27. };
  28. $connection_to_baidu->connect();
  29. };
  30. // 运行worker
  31. Worker::runAll();