Method: connect

void AsyncUdpConnection::connect()

Performs an asynchronous connection operation. This method returns immediately.


No parameters

Return Value

No return value


use Workerman\Worker;
use Workerman\Timer;
use Workerman\Connection\AsyncUdpConnection;
use Workerman\Connection\UdpConnection;

require_once __DIR__ . '/vendor/autoload.php';

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

Output after execution:

recv hello