OpenSwoole\Server->on

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\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 OpenSwoole\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

Last updated on September 1, 2022