Swoole\Server->on('packet', fn)

Declaration

<?php
Swoole\Server->on('packet', callback $callback)

Parameters

event

The event name

callback

callback function

Return

success

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

Description

Executue the callback function when the Server is receving UDP packages.

The callback function registered on the event packet is executed in the Worker Process which is receiving data.

Callback function

<?php
function (Swoole\Server $server, string $data, array $client_info) {

}
  • $data the UDP package received
  • $client_info, this data is an array

Example of $client_info:

Array                                                                                         
(                                                                                             
 [server_socket] => 4                                                                      
 [server_port] => 9501                                                                 
 [address] => 127.0.0.1                                                            
 [port] => 47306                                                               
)    

If the Server is listening on both TCP and UDP ports, the receive callback function is executed when TCP data is received and the packet callback function is executed when UDP data is received.

Example