国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

關(guān)于在控制器中使用SSE

南宮春水

問(wèn)題描述

前端使用event-source-polyfill來(lái)SSE請(qǐng)求,因?yàn)樾枰獢y帶token,后端在控制器中寫了讓如下方法。
其中$connection->getStatus()值都是:8。前端錯(cuò)誤:
截圖

<?php

namespace plugin\api\app\controller;

use plugin\api\app\model\UserModel;
use support\Db;
use support\Log;
use support\Request;
use support\Response;
use Tinywan\Jwt\JwtToken;
use Workbunny\WebmanIpAttribution\Location;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\ServerSentEvents;
use Workerman\Timer;

class UserController extends Base
{
    protected $userModel = null;

    public function __construct()
    {
        $this->userModel = new UserModel();
    }

    /**
     * 獲取用戶蘇哦有錢包余額
     * @param Request $request
     * @return Response
     * @throws \Webman\Push\PushException
     */
    public function getUserWalletBalance(Request $request)
    {
        $uid = JwtToken::getCurrentId();
        var_dump($request->header('accept'));
        // 使用SSE推送
        if ($request->header('accept') === 'text/event-stream') {
            $connection = $request->connection;
//            var_dump($connection);
            $connection->send(new Response(200, [
                'Content-Type' => 'text/event-stream'
            ], "\r\n"));

            //定時(shí)向客戶推送數(shù)據(jù)
            $timerId = Timer::add(1, function () use ($connection, &$timerId) {
                // 連接關(guān)閉的時(shí)候要將定時(shí)器刪除,避免定時(shí)器不斷累積導(dǎo)致內(nèi)存泄漏
                if ($connection->getStatus() !== TcpConnection::STATUS_ESTABLISHED) {
                    Timer::del($timer_id);
                    return;
                }
                // 發(fā)送message事件,事件攜帶的數(shù)據(jù)為hello,消息id可以不傳
                $connection->send(new ServerSentEvents(['event' => 'message', 'data' => 'hello', 'id' => 1]));
            });
            return;
        }
    }
}

為此你搜索到了哪些方案及不適用的原因

http://www.wtbis.cn/q/10107 按這里的意思?是不能在控制器里面實(shí)現(xiàn)嗎

845 1 2
1個(gè)回答

walkor 打賞

參考下面代碼

<?php
namespace app\controller;

use support\Request;
use support\Response;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\ServerSentEvents;
use Workerman\Timer;

class StreamController
{
    public function index(Request $request): Response
    {
        $connection = $request->connection;
        $id = Timer::add(1, function () use ($connection, &$id) {
            // 連接關(guān)閉時(shí),清除定時(shí)器
            if ($connection->getStatus() !== TcpConnection::STATUS_ESTABLISHED) {
                Timer::del($id);
            }
            $connection->send(new ServerSentEvents(['data' => 'hello']));
        });
        return response('', 200, [
            'Content-Type' => 'text/event-stream',
            'Cache-Control' => 'no-cache',
            'Connection' => 'keep-alive',
        ]);
    }

}

js

var source = new EventSource('http://127.0.0.1:8787/stream');
source.addEventListener('message', function (event) {
  var data = event.data;
  console.log(data); // 輸出 hello
}, false)
  • kzhzjdyw888 2025-01-22

    按上面示例定時(shí)器也執(zhí)行了,前端500 Internal Server Error

    1.后端路由
    Route::get('/sse', [StreamController::class, 'index'])->name('sse.stream');

    2.前端監(jiān)聽(tīng)
    const url = /api/sse?&Authorization=Bearer ${token};
    const { data } = useEventSource(url, [], {
    autoReconnect: {
    delay: 1000,
    onFailed() {
    console.error('重連失敗.');
    },
    retries: 3,
    },
    });

    500 Internal Server Error

  • walkor 2025-01-22

    500錯(cuò)誤看runtime下的日志

  • kzhzjdyw888 2025-01-22

    $connection->getStatus() 狀態(tài)直接8 跳出了

  • kzhzjdyw888 2025-01-22

    我是不是可以理解前后端不同域,狀態(tài)8直接斷開(kāi)鏈接了。

  • walkor 2025-01-22

    拷貝例子中的代碼運(yùn)行,包括前端代碼,同一個(gè)域名下測(cè)試,沒(méi)問(wèn)題后再改成自己的代碼

??