ledc/workerman-process

workerman进程启动器

v2.0.1 2024-10-14 12:53 UTC

This package is auto-updated.

Last update: 2024-11-14 13:14:24 UTC


README

安装

composer require ledc/workerman-process

使用

<?php

use Ledc\WorkermanProcess\Process;

require_once __DIR__ . '/vendor/autoload.php';

$process = [
    'websocket' => [
        //使能
        'enable' => true,
        //监听
        'listen' => 'websocket://0.0.0.0:2345',
        //上下文
        'context' => [],
        //worker支持的属性和回调属性
        'properties' => [
            // 事件回调
            // onWorkerStart
            'onWorkerStart'  => function ($worker) {

            },
            // onWorkerReload
            'onWorkerReload' => function ($worker) {

            },
            // onConnect
            'onConnect'      => function ($connection) {

            },
            // onMessage
            'onMessage'      => function ($connection, $data) {
                $connection->send('receive success');
            },
            // onClose
            'onClose'        => function ($connection) {

            },
            // onError
            'onError'        => function ($connection, $code, $msg) {
                echo "error [ $code ] $msg\n";
            },
        ],
        //业务进程:handler类
        'handler' => '',
        //业务进程:handler类的构造函数参数
        'constructor' => [],
    ],
    'http' => [
        //使能
        'enable' => true,
        //监听
        'listen' => 'http://0.0.0.0:2346',
        //上下文
        'context' => [],
        //worker支持的属性
        'properties' => [
            //属性
            'count' => 2,
            // 事件回调
            // onMessage
            'onMessage'      => function ($connection, $data) {
                $connection->send('receive success');
            },
        ],
        //业务进程:handler类
        'handler' => '',
        //业务进程:handler类的构造函数参数
        'constructor' => [],
    ],
];

foreach ($process as $name => $config) {
    Process::start($name, $config);
}

Process::runAll();

配置详情

配置节点properties支持workerman的所有属性和回调属性,详情参考官方文档:workerman 手册

业务handler类在子进程onWorkerStart时实例化,handler类支持workerman的所有回调属性;

配置生效情况

最佳实践

  • GatewayWorker,使用第一种方式启动
  • 继承Workerman\Worker开发的类,使用第一种方式启动
  • 未继承,使用第二种或第三种方式启动