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

        $server = new TcpServer(
            $this->swooleConfig['tcp']['host'] ?? self::DEFAULT_SWOOLE_HOST,
            $this->swooleConfig['tcp']['port'] ?? self::DEFAULT_SWOOLE_PORT
        );

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

        $server->on(
            'connect', function ($serv, $fd) {
                $this->writeln('Client:Connect.');
            }
        );

        $server->on(
            'receive', function ($serv, $fd, $from_id, $data) {
                $jsonData = JsonHelper::decode(str_replace(self::EOF, '', $data));
                if (isset($jsonData['handler'])) {
                    $jsonData['swoole_from_id'] = $from_id;
                    $handlerClass = $jsonData['handler'];
                    try {
                        $serv->send(
                            $fd,
                            Lb::app()->dispatchJob($handlerClass, $jsonData)
                        );
                    } catch (\Throwable $e) {
                        $serv->send($fd, 'Exception:' . $e->getTraceAsString());
                    }
                } else {
                    $serv->send($fd, 'Handler not exists');
                }
            }
        );

        $server->on(
            'close', function ($serv, $fd) {
                $this->writeln('Client: Close.');
            }
        );

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

        $server = new TcpServer(
            $this->swooleConfig['tcp']['host'] ?? self::DEFAULT_SWOOLE_HOST,
            $this->swooleConfig['tcp']['port'] ?? self::DEFAULT_SWOOLE_PORT
        );

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

        $server->on(
            'connect', function ($serv, $fd) {
                $this->writeln('Client:Connect.');
            }
        );

        $server->on(
            'receive', function ($serv, $fd, $from_id, $data) {
                $jsonData = JsonHelper::decode(str_replace(self::EOF, '', $data));
                if (isset($jsonData['handler'])) {
                    $jsonData['swoole_from_id'] = $from_id;
                    $handlerClass = $jsonData['handler'];
                    try {
                        $serv->send(
                            $fd,
                            Lb::app()->dispatchJob($handlerClass, $jsonData)
                        );
                    } catch (\Throwable $e) {
                        $serv->send($fd, 'Exception:' . $e->getTraceAsString());
                    }
                } else {
                    $serv->send($fd, 'Handler not exists');
                }
            }
        );

        $server->on(
            'close', function ($serv, $fd) {
                $this->writeln('Client: Close.');
            }
        );

        $server->start();
    }
public function tcp ( ) { $ this -> writeln ( 'Starting swoole tcp server...' ) ; $ server = new TcpServer ( $ this -> swooleConfig [ 'tcp' ] [ 'host' ] ?? self :: DEFAULT_SWOOLE_HOST , $ this -> swooleConfig [ 'tcp' ] [ 'port' ] ?? self :: DEFAULT_SWOOLE_PORT ) ; //防止粘包 $ server -> set ( [ 'open_eof_split' => true , 'package_eof' => self :: EOF , ] ) ; $ server -> on ( 'connect' , function ( $ serv , $ fd ) { $ this -> writeln ( 'Client:Connect.' ) ; } ) ; $ server -> on ( 'receive' , function ( $ serv , $ fd , $ from_id , $ data ) { $ jsonData = JsonHelper :: decode ( str_replace ( self :: EOF , '' , $ data ) ) ; if ( isset ( $ jsonData [ 'handler' ] ) ) { $ jsonData [ 'swoole_from_id' ] = $ from_id ; $ handlerClass = $ jsonData [ 'handler' ] ; try { $ serv -> send ( $ fd , Lb :: app ( ) -> dispatchJob ( $ handlerClass , $ jsonData ) ) ; } catch ( \ Throwable $ e ) { $ serv -> send ( $ fd , 'Exception:' . $ e -> getTraceAsString ( ) ) ; } } else { $ serv -> send ( $ fd , 'Handler not exists' ) ; } } ) ; $ server -> on ( 'close' , function ( $ serv , $ fd ) { $ this -> writeln ( 'Client: Close.' ) ; } ) ; $ server -> start ( ) ; }
Swoole Tcp Server
Swoole Tcp Server