Swoole Hook UDP

The SWOOLE_HOOK_UDP flag will enable coroutine support for UDP sockets and streams. Support was added in v4.2.0.

Example

<?php

Co::set(['hook_flags' => SWOOLE_HOOK_UDP]);

Co\run(function () {

    $socket = stream_socket_server(
        'udp://0.0.0.0:6666',
        $errno,
        $errstr,
        STREAM_SERVER_BIND
    );

    if (!$socket) {
        echo "$errstr ($errno)" . PHP_EOL;
        exit(1);
    }

    while (stream_socket_recvfrom($socket, 1, 0)) {
        // ...
    }
});