From 8b5f313284ee876f3627ab4a2eb6460072f12fd1 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sun, 23 Nov 2025 12:26:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3websocket-flv/fmp4/ts?= =?UTF-8?q?=E5=A4=9A=E8=B7=AF=E6=92=AD=E6=94=BE=E8=AF=B7=E6=B1=82pending?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#4553)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决等待流注册或播放鉴权hook回复时,zlm未及时回复http头(升级为websocket), 多个http请求并发导致pending的问题 --- src/Http/HttpSession.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 05a56488..b9b9109a 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -224,22 +224,22 @@ bool HttpSession::checkWebSocket() { headerOut["Sec-WebSocket-Protocol"] = _parser["Sec-WebSocket-Protocol"]; } - auto res_cb = [this, headerOut]() { - _live_over_websocket = true; - sendResponse(101, false, nullptr, headerOut, nullptr, true); + auto res_cb = []() { + // 改成先回复http头模式,以解决按需播放场景下websocket请求pending问题:#4553 }; - auto res_cb_flv = [this, headerOut]() mutable { - _live_over_websocket = true; + auto res_immediately = [this, headerOut]() mutable { headerOut.emplace("Cache-Control", "no-store"); sendResponse(101, false, nullptr, headerOut, nullptr, true); + _live_over_websocket = true; }; // 判断是否为websocket-flv [AUTO-TRANSLATED:31682d7a] // Determine whether it is websocket-flv - if (checkLiveStreamFlv(res_cb_flv)) { + if (checkLiveStreamFlv(res_cb)) { // 这里是websocket-flv直播请求 [AUTO-TRANSLATED:4bea5956] // This is a websocket-flv live request + res_immediately(); return true; } @@ -248,6 +248,7 @@ bool HttpSession::checkWebSocket() { if (checkLiveStreamTS(res_cb)) { // 这里是websocket-ts直播请求 [AUTO-TRANSLATED:8ab08dd6] // This is a websocket-ts live request + res_immediately(); return true; } @@ -256,6 +257,7 @@ bool HttpSession::checkWebSocket() { if (checkLiveStreamFMP4(res_cb)) { // 这里是websocket-fmp4直播请求 [AUTO-TRANSLATED:ccf0c1e2] // This is a websocket-fmp4 live request + res_immediately(); return true; } @@ -659,6 +661,23 @@ void HttpSession::sendResponse(int code, const HttpSession::KeyValue &header, const HttpBody::Ptr &body, bool no_content_length) { + if (_live_over_websocket) { + WebSocketHeader ws_header; + ws_header._fin = true; + ws_header._reserved = 0; + ws_header._opcode = WebSocketHeader::CLOSE; + ws_header._mask_flag = false; + uint16_t why = htons(0xFFFF & code); + std::string buffer; + buffer.append(reinterpret_cast(&why), 2); + if (body && code != 404) { + buffer.append(body->readData(body->remainSize())->toString()); + } else { + buffer.append("unknown reason"); + } + WebSocketSplitter::encode(ws_header, std::make_shared(std::move(buffer))); + return; + } GET_CONFIG(string, charSet, Http::kCharSet); GET_CONFIG(uint32_t, keepAliveSec, Http::kKeepAliveSecond);