Swoole Server on

public void Swoole\Server::on ( string $event_name , callable $callback )

Alias: Swoole\Server->on ( string $event_name , callable $callback )

Register callback function for the event

Parameter

  • $event the string of event to be registered
  • $callback the callable type variable

Check the full list of events

Check the four types of callbacks

Return

The result if it has been registered successfully

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();