swoole一键协程化(UDP Socket类型的stream)

2021-10-31
use Swoole\Coroutine;
use function Swoole\Coroutine\run;

// 一键协程化
Coroutine::set(['hook_flags'=> SWOOLE_HOOK_UDP]);

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)) {
    }
});

 

{/if}