mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
update:流控调试
This commit is contained in:
parent
7a4fcf42ea
commit
42625e0669
2 changed files with 6 additions and 21 deletions
|
|
@ -94,7 +94,9 @@ class ConnectionHandler:
|
|||
try:
|
||||
# 获取并验证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)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import random
|
|||
import difflib
|
||||
import re
|
||||
import traceback
|
||||
import time
|
||||
import threading
|
||||
import websockets
|
||||
|
||||
|
|
@ -144,9 +143,8 @@ class MusicHandler:
|
|||
try:
|
||||
# 将流式任务保存为实例属性
|
||||
self._current_stream_task = asyncio.current_task()
|
||||
total_sent = 0
|
||||
start_time = time.time()
|
||||
|
||||
# TODO 对发送音频进行流控,以避免过快的网络传输导致的卡顿
|
||||
for i, opus_packet in enumerate(opus_packets):
|
||||
if self.stop_event.is_set():
|
||||
# 如果被中断,先发送停止消息再退出
|
||||
|
|
@ -156,23 +154,8 @@ class MusicHandler:
|
|||
except:
|
||||
pass
|
||||
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)
|
||||
total_sent += len(opus_packet)
|
||||
except:
|
||||
break
|
||||
|
||||
if i % 100 == 0:
|
||||
logger.bind(tag=TAG).debug(f"已发送 {i}/{len(opus_packets)} 个数据包")
|
||||
|
||||
await asyncio.sleep(0.02) # 控制发送频率
|
||||
await conn.websocket.send(opus_packet)
|
||||
logger.bind(tag=TAG).info("发送完毕")
|
||||
except asyncio.CancelledError:
|
||||
logger.bind(tag=TAG).info("音乐播放被强制取消")
|
||||
finally:
|
||||
|
|
|
|||
Loading…
Reference in a new issue