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

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

        $ws->on(
            'open', function ($ws, $request) {
                $this->writeln('client-Connect.');
            }
        );

        $ws->on(
            'message', function ($ws, $frame) {
                $jsonData = JsonHelper::decode($frame->data);
                if (isset($jsonData['handler'])) {
                    $jsonData['swoole_frame'] = $frame;
                    $handlerClass = $jsonData['handler'];
                    try {
                        $ws->push(
                            $frame->fd,
                            Lb::app()->dispatchJob($handlerClass, $jsonData)
                        );
                    } catch (\Throwable $e) {
                        $ws->push(
                            $frame->fd,
                            'Exception:' . $e->getTraceAsString()
                        );
                    }
                } else {
                    $ws->push(
                        $frame->fd,
                        'Handler not exists'
                    );
                }
            }
        );

        $ws->on(
            'close', function ($ws, $fd) {
                $this->writeln('client-closed');
            }
        );

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

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

        $ws->on(
            'open', function ($ws, $request) {
                $this->writeln('client-Connect.');
            }
        );

        $ws->on(
            'message', function ($ws, $frame) {
                $jsonData = JsonHelper::decode($frame->data);
                if (isset($jsonData['handler'])) {
                    $jsonData['swoole_frame'] = $frame;
                    $handlerClass = $jsonData['handler'];
                    try {
                        $ws->push(
                            $frame->fd,
                            Lb::app()->dispatchJob($handlerClass, $jsonData)
                        );
                    } catch (\Throwable $e) {
                        $ws->push(
                            $frame->fd,
                            'Exception:' . $e->getTraceAsString()
                        );
                    }
                } else {
                    $ws->push(
                        $frame->fd,
                        'Handler not exists'
                    );
                }
            }
        );

        $ws->on(
            'close', function ($ws, $fd) {
                $this->writeln('client-closed');
            }
        );

        $ws->start();
    }
public function websocket ( ) { $ this -> writeln ( 'Starting swoole websocket server...' ) ; $ ws = new WebsocketServer ( $ this -> swooleConfig [ 'ws' ] [ 'host' ] ?? self :: DEFAULT_SWOOLE_HOST , $ this -> swooleConfig [ 'ws' ] [ 'port' ] ?? self :: DEFAULT_SWOOLE_PORT ) ; $ ws -> on ( 'open' , function ( $ ws , $ request ) { $ this -> writeln ( 'client-Connect.' ) ; } ) ; $ ws -> on ( 'message' , function ( $ ws , $ frame ) { $ jsonData = JsonHelper :: decode ( $ frame -> data ) ; if ( isset ( $ jsonData [ 'handler' ] ) ) { $ jsonData [ 'swoole_frame' ] = $ frame ; $ handlerClass = $ jsonData [ 'handler' ] ; try { $ ws -> push ( $ frame -> fd , Lb :: app ( ) -> dispatchJob ( $ handlerClass , $ jsonData ) ) ; } catch ( \ Throwable $ e ) { $ ws -> push ( $ frame -> fd , 'Exception:' . $ e -> getTraceAsString ( ) ) ; } } else { $ ws -> push ( $ frame -> fd , 'Handler not exists' ) ; } } ) ; $ ws -> on ( 'close' , function ( $ ws , $ fd ) { $ this -> writeln ( 'client-closed' ) ; } ) ; $ ws -> start ( ) ; }
Swoole Websocket Server
Swoole Websocket Server