• udp应用
    • udp服务器开启,在EasySwooleEvent.php中进行创建服务
    • udp客户端

    udp应用

    udp服务器开启,在EasySwooleEvent.php中进行创建服务

    1. public static function mainServerCreate(EventRegister $register)
    2. {
    3. $server = ServerManager::getInstance()->getSwooleServer();
    4. $subPort = $server->addListener('0.0.0.0','9601',SWOOLE_UDP);
    5. $subPort->on('packet',function (\swoole_server $server, string $data, array $client_info){
    6. var_dump($data);
    7. });
    8. }

    udp客户端

    1. public static function mainServerCreate(EventRegister $register)
    2. {
    3. //添加自定义进程做定时udp发送
    4. $server->addProcess(new \swoole_process(function (\swoole_process $process){
    5. //服务正常关闭
    6. $process::signal(SIGTERM,function ()use($process){
    7. $process->exit(0);
    8. });
    9. //默认5秒广播一次
    10. \Swoole\Timer::tick(5000,function (){
    11. if($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))
    12. {
    13. socket_set_option($sock,SOL_SOCKET,SO_BROADCAST,true);
    14. $msg= '123456';
    15. socket_sendto($sock,$msg,strlen($msg),0,'255.255.255.255',9602);//广播地址
    16. socket_close($sock);
    17. }
    18. });
    19. }));
    20. }