send Method

void AsyncUdpConnection::send(string $data)

Performs an asynchronous connection operation. This method will return immediately.

Parameters

$data
The data to be sent to the server. The size of the data must not exceed 65507 bytes (the maximum transmission size of a single UDP packet is 65507 bytes), otherwise the sending will fail.

Return Value

No return value

Example

use Workerman\Worker;
use Workerman\Timer;
use Workerman\Connection\AsyncUdpConnection;
use Workerman\Connection\UdpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('udp://0.0.0.0:1234');
$worker->onWorkerStart = function(){
    // After 1 second, start a UDP client, connect to port 1234 and send the string hi
    Timer::add(1, function(){
        $udp_connection = new AsyncUdpConnection('udp://127.0.0.1:1234');
        $udp_connection->onConnect = function(AsyncUdpConnection $udp_connection){
            $udp_connection->send('hi');
        };
        $udp_connection->onMessage = function(AsyncUdpConnection $udp_connection, $data){
            // Received data hello returned from the server
            echo "recv $data\r\n";
            // Close the connection
            $udp_connection->close();
        };
        $udp_connection->connect();
    }, null, false);
};
$worker->onMessage = function(UdpConnection $connection, $data)
{
    // Received data from AsyncUdpConnection client, return the string hello
    $connection->send("hello");
};
Worker::runAll();             

After execution, it prints something like:

recv hello