1、在vendor/workerman/文件夹下建立server.php文件,内容如下:
<?php use Workerman\Worker; require_once __DIR__ . '/workerman/Autoloader.php';
//如果使用ssl $context = array( 'ssl' => array( // 请使用绝对路径 'local_cert' => '*.pem', // 也可以是crt文件 'local_pk' => '*.key', 'verify_peer' => false, // 'allow_self_signed' => true, //如果是自签名证书需要开启此选项 ), ); $ws_worker = new Worker("websocket://0.0.0.0:1234", $context); $ws_worker->count = 1; //启用内部通讯服务 $ws_worker->onWorkerStart = function ($ws_worker) { $context = array( 'ssl' => array( // 请使用绝对路径 'local_cert' => '*.pem', // 也可以是crt文件 'local_pk' => '*.key', 'verify_peer' => false, // 'allow_self_signed' => true, //如果是自签名证书需要开启此选项 ), ); $inner_text_worker = new Worker('Text://0.0.0.0:5678', $context); $inner_text_worker->onMessage = function ($connection_inner, $buffer) { global $ws_worker; foreach ($ws_worker->connections as $connection) { $connection->send($buffer); } $connection_inner->send($data); }; $inner_text_worker->listen(); }; Worker::runAll();
2、后台运行该文件
sudo nohup php ***/vendor/workerman/server.php start &