From 42625e06695a0345447da2dc2a3994530fc71b66 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Tue, 25 Feb 2025 23:44:07 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=B5=81=E6=8E=A7=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/connection.py | 4 +++- core/handle/musicHandler.py | 23 +++-------------------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/core/connection.py b/core/connection.py index 3507f6d4d..58e27d204 100644 --- a/core/connection.py +++ b/core/connection.py @@ -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) diff --git a/core/handle/musicHandler.py b/core/handle/musicHandler.py index 6ab3e77d0..f89c47345 100644 --- a/core/handle/musicHandler.py +++ b/core/handle/musicHandler.py @@ -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: