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 a server event.

Events

Check the four different 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();

Repeated calls to the same event will override any previously set callbacks