public function udp()
    {
        $this->writeln('Starting swoole udp server...');

        $udpServer = new TcpServer(
            $this->swooleConfig['upd']['host'] ?? self::DEFAULT_SWOOLE_HOST,
            $this->swooleConfig['upd']['port'] ?? self::DEFAULT_SWOOLE_PORT,
            SWOOLE_PROCESS,
            SWOOLE_SOCK_UDP
        );

        //防止粘包
        $udpServer->set(
            [
            'open_eof_split' => true,
            'package_eof' => self::EOF,
            ]
        );

        $udpServer->on(
            'Packet', function ($serv, $data, $clientInfo) {
                $clientAddress = $clientInfo['address'];
                $clientPort = $clientInfo['port'];
                $jsonData = JsonHelper::decode(str_replace(self::EOF, '', $data));
                if (isset($jsonData['handler'])) {
                    $jsonData['swoole_client_info'] = $clientInfo;
                    $handlerClass = $jsonData['handler'];
                    try {
                        $serv->sendto(
                            $clientAddress,
                            $clientPort,
                            Lb::app()->dispatchJob($handlerClass, $jsonData)
                        );
                    } catch (\Throwable $e) {
                        $serv->sendto(
                            $clientAddress,
                            $clientPort,
                            'Exception:' . $e->getTraceAsString()
                        );
                    }
                } else {
                    $serv->sendto(
                        $clientAddress,
                        $clientPort,
                        'Handler not exists'
                    );
                }
            }
        );

        $udpServer->start();
    }
public function udp()
    {
        $this->writeln('Starting swoole udp server...');

        $udpServer = new TcpServer(
            $this->swooleConfig['upd']['host'] ?? self::DEFAULT_SWOOLE_HOST,
            $this->swooleConfig['upd']['port'] ?? self::DEFAULT_SWOOLE_PORT,
            SWOOLE_PROCESS,
            SWOOLE_SOCK_UDP
        );

        //防止粘包
        $udpServer->set(
            [
            'open_eof_split' => true,
            'package_eof' => self::EOF,
            ]
        );

        $udpServer->on(
            'Packet', function ($serv, $data, $clientInfo) {
                $clientAddress = $clientInfo['address'];
                $clientPort = $clientInfo['port'];
                $jsonData = JsonHelper::decode(str_replace(self::EOF, '', $data));
                if (isset($jsonData['handler'])) {
                    $jsonData['swoole_client_info'] = $clientInfo;
                    $handlerClass = $jsonData['handler'];
                    try {
                        $serv->sendto(
                            $clientAddress,
                            $clientPort,
                            Lb::app()->dispatchJob($handlerClass, $jsonData)
                        );
                    } catch (\Throwable $e) {
                        $serv->sendto(
                            $clientAddress,
                            $clientPort,
                            'Exception:' . $e->getTraceAsString()
                        );
                    }
                } else {
                    $serv->sendto(
                        $clientAddress,
                        $clientPort,
                        'Handler not exists'
                    );
                }
            }
        );

        $udpServer->start();
    }
public function udp ( ) { $ this -> writeln ( 'Starting swoole udp server...' ) ; $ udpServer = new TcpServer ( $ this -> swooleConfig [ 'upd' ] [ 'host' ] ?? self :: DEFAULT_SWOOLE_HOST , $ this -> swooleConfig [ 'upd' ] [ 'port' ] ?? self :: DEFAULT_SWOOLE_PORT , SWOOLE_PROCESS , SWOOLE_SOCK_UDP ) ; //防止粘包 $ udpServer -> set ( [ 'open_eof_split' => true , 'package_eof' => self :: EOF , ] ) ; $ udpServer -> on ( 'Packet' , function ( $ serv , $ data , $ clientInfo ) { $ clientAddress = $ clientInfo [ 'address' ] ; $ clientPort = $ clientInfo [ 'port' ] ; $ jsonData = JsonHelper :: decode ( str_replace ( self :: EOF , '' , $ data ) ) ; if ( isset ( $ jsonData [ 'handler' ] ) ) { $ jsonData [ 'swoole_client_info' ] = $ clientInfo ; $ handlerClass = $ jsonData [ 'handler' ] ; try { $ serv -> sendto ( $ clientAddress , $ clientPort , Lb :: app ( ) -> dispatchJob ( $ handlerClass , $ jsonData ) ) ; } catch ( \ Throwable $ e ) { $ serv -> sendto ( $ clientAddress , $ clientPort , 'Exception:' . $ e -> getTraceAsString ( ) ) ; } } else { $ serv -> sendto ( $ clientAddress , $ clientPort , 'Handler not exists' ) ; } } ) ; $ udpServer -> start ( ) ; }
Swoole UDP Server
Swoole UDP Server