Built-in event

Swoft built-in events, based on swoole's callback handling, extend some of the available server events and start the runtime base to enhance customization

Server event

Based on the swoole callback processing, some available server events are extended to provide a more refined operation space.

  • ServerEvent::BEFORE_SETTING before calling the swoole server's setting() method
  • ServerEvent::BEFORE_BIND_EVENT before calling the swoole server's on() method to bind the swoole callback
  • ServerEvent::BEFORE_BIND_LISTENER before calling the swoole server's listen() method to add a multiport listener
  • ServerEvent::AFTER_ADDED_LISTENER Every time you call the swoole server's listen() method to successfully add a port listener (adding multiple ports is simple, it will be called multiple times)
  • ServerEvent::BEFORE_START before starting the server by calling the start() method of the swoole server
  • ServerEvent::TASK_PROCESS_START only when the task process of the swoole server starts
  • ServerEvent::WORK_PROCESS_START only when the worker process of the swoole server starts
 <?php declare(strict_types=1);

namespace Swoft\Server;

/**
 * Class ServerEvent
 *
 * @since 2.0
 */
final class ServerEvent
{
    /**
     * Before set swoole settings
     */
    public const BEFORE_SETTING = 'swoft.server.before.setting';

    /**
     * Before bind swoole events
     */
    public const BEFORE_BIND_EVENT = 'swoft.server.bind.event';

    /**
     * Before bind listener(s)
     */
    public const BEFORE_BIND_LISTENER = 'swoft.server.bind.listener.before';

    /**
     * After each listener is successfully added
     */
    public const AFTER_ADDED_LISTENER = 'swoft.server.added.listener.after';

    /**
     * Swoft before start server event
     */
    public const BEFORE_START = 'swoft.server.start.before';

    /**
     * On task process start event
     */
    public const TASK_PROCESS_START = 'swoft.process.task.start';

    /**
     * On work process start event
     */
    public const WORK_PROCESS_START = 'swoft.process.work.start';

    /**
     * on user process start event
     */
    public const USER_PROCESS_START = 'swoft.process.user.start';
} 

Use example

We can register a custom process before the swoole server starts, so that the process can have server hosting.

  • There is no need to execute start. The process is automatically created when the server starts, and the specified child process function is executed.
  • When the server is shut down, the SIGTERM signal is sent to the user process to close the user process.
  • The custom process will be hosted to the Manager process, and if a fatal error occurs, the Manager process will recreate one.
 <?php declare(strict_types=1);

namespace App\Listener;

use App\Process\MyProcess;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Server\ServerEvent;

/**
 * Class AttachMyProcessHandler
 * @Listener(ServerEvent::BEFORE_START)
 */
class AttachMyProcessHandler implements EventHandlerInterface
{
    /**
     * @param EventInterface $event
     */
    public function handle(EventInterface $event): void
    {
        $swooleServer = $event->target->getSwooleServer();

        $process = bean(MyProcess::class);

        $swooleServer->addProcess($process->create());
    }
} 
 <?php declare(strict_types=1);

namespace App\Process;

use Swoft\Event\Annotation\Mapping\Listener;
use Swoole\Process;

/**
 * Class MyProcess
 * @Bean()
 */
class MyProcess
{
    public function create(): Process
    {
        $process = new Process([$this, 'handle']);

        return $process;
    }

    public function handle(Process $process)
    {
        CLog::info('my-process started');

        // 用户进程实现了广播功能,循环接收管道消息,并发给服务器的所有连接
        while (true) {
            $msg = $process->read();
            foreach($server->connections as $conn) {
                $server->send($conn, $msg);
            }
        }
    }
} 

Swoft base event

Provides some Swoft startup initialization after completion, as well as some special events inside Swoft

  • SwoftEvent::APP_INIT_COMPLETE when the swowt initialization is complete (the configuration is loaded and the container has been initialized)
 <?php declare(strict_types=1);

namespace Swoft;

/**
 * Class SwoftEvent
 * @since 2.0
 */
final class SwoftEvent
{
    /**
     * Swoft init complete
     */
    public const APP_INIT_COMPLETE  = 'swoft.init.complete';

    /**
     * Session complete
     *  - webSocket connection close
     */
    public const SESSION_COMPLETE = 'swoft.session.complete';

    /**
     * Coroutine complete
     */
    public const COROUTINE_COMPLETE = 'swoft.co.complete';

    /**
     * Coroutine destroy
     */
    public const COROUTINE_DESTROY = 'swoft.co.destroy';

    /**
     * Coroutine defer
     */
    public const COROUTINE_DEFER = 'swoft.co.defer';

    /**
     * Worker shutdown
     */
    public const WORKER_SHUTDOWN = 'swoft.worker.shutdown';
} 
/docs/2.x/en/event/swoft-events.html
progress-bar