webman1.6 在控制器中使用SSE报错

fisha

问题描述

我想实现一个安装时实时显示安装进度的功能,想在控制器中使用sse,于是在搜了一下帖子,找到了这个帖子
https://www.workerman.net/q/11773 按照他的样子测试了一下,然后报错了
截图

版本信息

"require": {
    "php": ">=7.2",
    "workerman/webman-framework": "1.6",
    "monolog/monolog": "^2.0",
    "ext-json": "*",
    "illuminate/database": "^9.52",
    "illuminate/pagination": "^9.52",
    "illuminate/events": "^9.52",
    "symfony/var-dumper": "^6.0",
    "laravel/serializable-closure": "^1.3",
    "workerman/validation": "^3.1"
  },

程序代码

    public function install(Request $request): Response
    {
        if ($request->header('accept') === 'text/event-stream') {
            $connection = $request->connection;
            $connection->send(new Response(200, ['Content-Type' => 'text/event-stream'], "\r\n"));
            // 定时向客户端推送数据
            $timer_id = Timer::add(2, function () use ($connection, &$timer_id) {
                // 连接关闭的时候要将定时器删除,避免定时器不断累积导致内存泄漏
                if ($connection->getStatus() !== TcpConnection::STATUS_ESTABLISHED) {
                    Timer::del($timer_id);
                    return;
                }
                // 发送message事件,事件携带的数据为hello,消息id可以不传
                $connection->send(new ServerSentEvents(['event' => 'message', 'data' => 'hello', 'id' => 1]));
            });
        } else {
            /**
             * 验证参数
             */
            try {
                $v = v::input($request->post(), [
                    'title' => v::notEmpty()->length(0, 50)->setName('title'),
                    'username' => v::notEmpty()->length(0, 50)->setName('username'),
                    'password' => v::notEmpty()->length(8, 50)->setName('password')
                ]);
            } catch (ValidationException $e) {
                return badRequest($e->getMessage());
            }
        }

    }

报错信息

HTTP/1.1 500 Internal Server Error
Server: workerman
Content-Type: text/html;charset=utf-8
Content-Length: 2098
Connection: keep-alive

Error: Call to a member function getRemoteIp() on null in F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\webman-framework\src\Http\Request.php:178
Stack trace:
#0 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\webman-framework\src\Http\Request.php(215): Webman\Http\Request->getRemoteIp()
#1 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\webman-framework\src\Exception\ExceptionHandler.php(68): Webman\Http\Request->getRealIp()
#2 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\webman-framework\src\support\exception\Handler.php(34): Webman\Exception\ExceptionHandler->report(Object(Illuminate\Database\QueryException))
#3 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\webman-framework\src\App.php(277): support\exception\Handler->report(Object(Illuminate\Database\QueryException))
#4 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\webman-framework\src\App.php(178): Webman\App::exceptionResponse(Object(Illuminate\Database\QueryException), Object(support\Request))
#5 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\workerman\Connection\TcpConnection.php(646): Webman\App->onMessage(Object(Workerman\Connection\TcpConnection), Object(support\Request))
#6 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\workerman\Events\Select.php(311): Workerman\Connection\TcpConnection->baseRead(Resource id #209)
#7 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\workerman\Worker.php(1569): Workerman\Events\Select->loop()
#8 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\workerman\Worker.php(1486): Workerman\Worker::forkWorkersForWindows()
#9 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\workerman\Worker.php(561): Workerman\Worker::forkWorkers()
#10 F:\project\YunTower\YunTowerServerMonitor\backend\vendor\workerman\webman-framework\src\support\App.php(137): Workerman\Worker::runAll()
#11 F:\project\YunTower\YunTowerServerMonitor\backend\start.php(4): support\App::run()
#12 {main}
225 2 0
2个回答

walkor 打赏

从调用栈看业务产生了一个 Illuminate\Database\QueryException 异常。

  • 暂无评论
南宫春水

楼主,你好,请问你控制器能连接成功吗

  • 暂无评论
×
🔝