Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
When running a OpenSwoole server, if you enable event_object
and set it to true
, OpenSwoole will pass objects to your event callback functions instead of individual arguments.
To understand this change better, let's take a look at some examples but for more information, see the server configuration for event_object
.
For the following events, they will be passed a OpenSwoole\Server\Event
object:
Connect
Receive
Close
<?php
$server->on('Connect', function(OpenSwoole\Server $server, OpenSwoole\Server\Event $event)
{
var_dump($event);
});
For the server Packet
event, it will receive a OpenSwoole\Server\Packet
object:
<?php
$server->on('Packet', function (OpenSwoole\Server $server, OpenSwoole\Server\Packet $Packet)
{
var_dump($Packet);
});
For the server PipeMessage
event, it will receive a OpenSwoole\Server\PipeMessage
object:
<?php
$server->on('PipeMessage', function(OpenSwoole\Server $server, OpenSwoole\Server\PipeMessage $msg)
{
var_dump($msg);
$object = $msg->data;
$server->sendto($object->address, $object->port, $object->data, $object->server_socket);
});
For the WorkerError
event, it will receive a OpenSwoole\Server\StatusInfo
object:
<?php
$server->on('WorkerError', function(OpenSwoole\Server $server, OpenSwoole\Server\StatusInfo $info)
{
var_dump($info);
});
For the Task
event, it will receive a OpenSwoole\Server\Task
object:
<?php
$server->on('Task', function(OpenSwoole\Server $server, OpenSwoole\Server\Task $task)
{
var_dump($task);
});
For the task Finish
event, it will receive a OpenSwoole\Server\TaskResult
object:
<?php
$server->on('Finish', function(OpenSwoole\Server $server, OpenSwoole\Server\TaskResult $result)
{
var_dump($result);
});