title: mainServerCreate meta:

  • name: description content: 主服务创建事件
  • name: keywords content: swoole|swoole extension|swoole framework|EasySwoole|swoole|mainServerCreate

mainServerCreateEvent

The function prototype

  1. @param \EasySwoole\EasySwoole\Swoole\EventRegister $register
  2. public static function mainServerCreate(EventRegister $register)
  3. {
  4. }

Finished work

At the time of executing the event, the following work has been completed:

  • Framework initializes events
  • The configuration file is loaded
  • The main Swoole Server is created successfully
  • Main Swoole Server registered its default onRequest, onTask, onFinish events.。

Processable content

Register the main service callback event

For example, register the onWorkerStart event for the main service

  1. $register->add($register::onWorkerStart,function (\swoole_server $server,int $workerId){
  2. var_dump($workerId.'start');
  3. });

For example, add an onMessage event to the main service

  1. // In WebSocket mode message events must be registered and handed in
  2. $register->set(EventRegister::onMessage, function (\swoole_websocket_server $server, \swoole_websocket_frame $frame) {
  3. var_dump($frame);
  4. });

::: warning The set method is different from the add method. The set method overrides the previously configured event callback, while the add method adds a new callback :::

Add a custom process

::: tip Detailed operations can be viewed in the basic use -> custom process :::

  1. ServerManager::getInstance()->getSwooleServer()->addProcess((new Test('test_process'))->getProcess());

::: warning Test is a subclass of the EasySwoole\Component\Process\AbstractProcess'abstract class :::

Add a subservice listener

  1. $subPort = ServerManager::getInstance()->getSwooleServer()->addListener('0.0.0.0',9503,SWOOLE_TCP);
  2. $subPort->on('receive',function (\swoole_server $server, int $fd, int $reactor_id, string $data){
  3. var_dump($data);
  4. });

::: warning Refer to different ways of writing Demo branches event: The demo branch :::

Call the coroutine API before starting

  1. use Swoole\Coroutine\Scheduler;
  2. $scheduler = new Scheduler();
  3. $scheduler->add(function() {
  4. /* Call the coroutine API */
  5. });
  6. $scheduler->start();
  7. //Clear all timers
  8. \Swoole\Timer::clearAll();