fix: fix voice interupt
This commit is contained in:
@ -28,6 +28,7 @@ class BeaverTerminalConnectionClosed(BeaverTerminalError):
|
||||
@dataclass
|
||||
class MessageIdGenerator:
|
||||
peer_id: str
|
||||
nonce: str | None = None
|
||||
initial_counter: int = 0
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
@ -35,6 +36,8 @@ class MessageIdGenerator:
|
||||
|
||||
def next_id(self) -> str:
|
||||
self.counter += 1
|
||||
if self.nonce:
|
||||
return f"{self.peer_id}-{self.nonce}-{self.counter:06d}"
|
||||
return f"{self.peer_id}-{self.counter:06d}"
|
||||
|
||||
|
||||
@ -77,15 +80,22 @@ class BeaverTerminalClient:
|
||||
async def connect(self) -> None:
|
||||
await self._close_websocket()
|
||||
session = self._ensure_http_session()
|
||||
self._ws = await session.ws_connect(self._url)
|
||||
await self._send_json(
|
||||
build_connect_frame(peer_id=self._peer_id, device_name=self._device_name)
|
||||
)
|
||||
frame = await self._receive_json()
|
||||
if frame.get("type") != "connected":
|
||||
raise BeaverTerminalError(f"expected connected frame, received {frame!r}")
|
||||
session_id = frame.get("session_id")
|
||||
self.session_id = session_id if isinstance(session_id, str) else None
|
||||
try:
|
||||
self._ws = await session.ws_connect(self._url)
|
||||
await self._send_json(
|
||||
build_connect_frame(peer_id=self._peer_id, device_name=self._device_name)
|
||||
)
|
||||
frame = await self._receive_json()
|
||||
if frame.get("type") != "connected":
|
||||
raise BeaverTerminalError(f"expected connected frame, received {frame!r}")
|
||||
session_id = frame.get("session_id")
|
||||
self.session_id = session_id if isinstance(session_id, str) else None
|
||||
except (aiohttp.ClientError, asyncio.TimeoutError, BeaverTerminalConnectionClosed) as exc:
|
||||
await self._cleanup_failed_connection()
|
||||
raise BeaverTerminalConnectionClosed("failed to connect to Beaver websocket") from exc
|
||||
except Exception:
|
||||
await self._cleanup_failed_connection()
|
||||
raise
|
||||
|
||||
async def send_text(self, text: str) -> str:
|
||||
for attempt in range(2):
|
||||
@ -153,6 +163,13 @@ class BeaverTerminalClient:
|
||||
await self._ws.close()
|
||||
self._ws = None
|
||||
|
||||
async def _cleanup_failed_connection(self) -> None:
|
||||
await self._close_websocket()
|
||||
self.session_id = None
|
||||
if self._owned_session and self._http_session is not None:
|
||||
await self._http_session.close()
|
||||
self._http_session = None
|
||||
|
||||
def _websocket_is_open(self) -> bool:
|
||||
return self._ws is not None and not self._ws.closed
|
||||
|
||||
|
||||
Reference in New Issue
Block a user