Swoole\Server->on

Declaration

<?php
Swoole\Server->on ( string $event , callable $callback )

Parameters

event

The event name

callback

callback function

Return

success

if success, it returns TRUE, otherwise it returns FALSE.

Description

Register callback function for the event.

Events

  • Swoole\Server->on('start', fn)
  • Swoole\Server->on('shutdown', fn)
  • Swoole\Server->on('workerstart', fn)
  • Swoole\Server->on('workerstop', fn)
  • Swoole\Server->on('timer', fn)
  • Swoole\Server->on('connect', fn)
  • Swoole\Server->on('receive', fn)
  • Swoole\Server->on('packet', fn)
  • Swoole\Server->on('close', fn)
  • Swoole\Server->on('task', fn)
  • Swoole\Server->on('finish', fn)
  • Swoole\Server->on('pipemessage', fn)
  • Swoole\Server->on('workererror', fn)
  • Swoole\Server->on('managerstart', fn)
  • Swoole\Server->on('managerstop', fn)
  • Swoole\Server->on('beforereload', fn)
  • Swoole\Server->on('afterreload', fn)

    Check the four types of callbacks

    Example

    <?php
    $server = new Swoole\Server("127.0.0.1", 9501);
    $server->on('connect', function ($server, $fd){
        echo "New connection established: #{$fd}.\n";
    });
    $server->on('receive', function ($server, $fd, $from_id, $data) {
        $server->send($fd, "Echo to #{$fd}: \n".$data);
        $server->close($fd);
    });
    $server->on('close', function ($server, $fd) {
        echo "Connection closed: #{$fd}.\n";
    });
    $server->start();