update:流控调试

This commit is contained in:
hrz 2025-02-25 23:44:07 +08:00
parent 7a4fcf42ea
commit 42625e0669
2 changed files with 6 additions and 21 deletions

View file

@ -94,7 +94,9 @@ class ConnectionHandler:
try: try:
# 获取并验证headers # 获取并验证headers
self.headers = dict(ws.request.headers) self.headers = dict(ws.request.headers)
self.logger.bind(tag=TAG).info(f"New connection request - Headers: {self.headers}") # 获取客户端ip地址
client_ip = ws.remote_address[0]
self.logger.bind(tag=TAG).info(f"{client_ip} conn - Headers: {self.headers}")
# 进行认证 # 进行认证
await self.auth.authenticate(self.headers) await self.auth.authenticate(self.headers)

View file

@ -6,7 +6,6 @@ import random
import difflib import difflib
import re import re
import traceback import traceback
import time
import threading import threading
import websockets import websockets
@ -144,9 +143,8 @@ class MusicHandler:
try: try:
# 将流式任务保存为实例属性 # 将流式任务保存为实例属性
self._current_stream_task = asyncio.current_task() self._current_stream_task = asyncio.current_task()
total_sent = 0
start_time = time.time()
# TODO 对发送音频进行流控,以避免过快的网络传输导致的卡顿
for i, opus_packet in enumerate(opus_packets): for i, opus_packet in enumerate(opus_packets):
if self.stop_event.is_set(): if self.stop_event.is_set():
# 如果被中断,先发送停止消息再退出 # 如果被中断,先发送停止消息再退出
@ -156,23 +154,8 @@ class MusicHandler:
except: except:
pass pass
break break
expected_time = start_time + (i * packet_duration / 1000)
current_time = time.time()
if current_time < expected_time:
await asyncio.sleep(expected_time - current_time)
try:
await conn.websocket.send(opus_packet) await conn.websocket.send(opus_packet)
total_sent += len(opus_packet) logger.bind(tag=TAG).info("发送完毕")
except:
break
if i % 100 == 0:
logger.bind(tag=TAG).debug(f"已发送 {i}/{len(opus_packets)} 个数据包")
await asyncio.sleep(0.02) # 控制发送频率
except asyncio.CancelledError: except asyncio.CancelledError:
logger.bind(tag=TAG).info("音乐播放被强制取消") logger.bind(tag=TAG).info("音乐播放被强制取消")
finally: finally: