transport

说明:

  1. string Worker::$transport

设置当前Worker实例所使用的传输层协议,目前只支持3种(tcp、udp、ssl)。不设置默认为tcp。

注意:ssl需要Workerman版本>=3.3.7

范例 1

  1. use Workerman\Worker;
  2. require_once __DIR__ . '/Workerman/Autoloader.php';
  3. $worker = new Worker('text://0.0.0.0:8484');
  4. // 使用udp协议
  5. $worker->transport = 'udp';
  6. $worker->onMessage = function($connection, $data)
  7. {
  8. $connection->send('Hello');
  9. };
  10. // 运行worker
  11. Worker::runAll();

范例 2

  1. <?php
  2. require_once __DIR__ . '/Workerman/Autoloader.php';
  3. use Workerman\Worker;
  4. // 证书最好是申请的证书
  5. $context = array(
  6. 'ssl' => array(
  7. 'local_cert' => '/etc/nginx/conf.d/ssl/server.pem', // 也可以是crt文件
  8. 'local_pk' => '/etc/nginx/conf.d/ssl/server.key',
  9. )
  10. );
  11. // 这里设置的是websocket协议
  12. $worker = new Worker('websocket://0.0.0.0:4431', $context);
  13. // 设置transport开启ssl,websocket+ssl即wss
  14. $worker->transport = 'ssl';
  15. $worker->onMessage = function($con, $msg) {
  16. $con->send('ok');
  17. };
  18. Worker::runAll();