Skip to content

Commit

Permalink
Some changes to revocation and resubscribing on websocket.
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed Feb 13, 2025
1 parent 786c9bb commit 6f16ea5
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions twitchio/eventsub/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Websocket:
"_backoff",
"_client",
"_closed",
"_closing",
"_connecting",
"_connection_tasks",
"_heartbeat",
Expand Down Expand Up @@ -124,6 +125,7 @@ def __init__(

self._connecting: bool = False
self._closed: bool = False
self._closing: bool = False

self._connection_tasks: set[asyncio.Task[None]] = set()

Expand Down Expand Up @@ -238,7 +240,10 @@ async def connect(self, *, url: str | None = None, reconnect: bool = False, fail
async def _resubscribe(self) -> None:
assert self._session_id

for identifier, sub in self._subscriptions.copy().items():
old_subs = self._subscriptions.copy()
self._subscriptions.clear()

for identifier, sub in old_subs.items():
sub["transport"]["session_id"] = self._session_id

try:
Expand Down Expand Up @@ -407,6 +412,9 @@ async def _process_revocation(self, data: RevocationMessage) -> None:
self._client.dispatch(event="subscription_revoked", payload=payload)

self._subscriptions.pop(payload.id, None)
if not self._subscriptions:
logger.info("Closing websocket '%s' due to no remaining subscriptions.", self)
return await self.close()

async def _process_notification(self, data: NotificationMessage) -> None:
sub_type = data["metadata"]["subscription_type"]
Expand All @@ -429,6 +437,8 @@ def _cleanup(self, closed: bool = True) -> None:
sockets.pop(self.session_id or "", None)

async def close(self, cleanup: bool = True) -> None:
self._closing = True

if cleanup:
self._cleanup()

Expand All @@ -454,4 +464,5 @@ async def close(self, cleanup: bool = True) -> None:
self._listen_task = None
self._socket = None

logger.debug("Successfully closed eventsub websocket: <%s>", self)
logger.info("Successfully closed eventsub websocket: <%s>", self)
self._closing = False

0 comments on commit 6f16ea5

Please sign in to comment.