Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2023-11-28 17:05:43 -08:00
parent 738659dfbc
commit 098e1fa97d
6 changed files with 530 additions and 451 deletions

View File

@ -1,5 +1,5 @@
import json
from typing import Any, Dict
from typing import Any, Dict, Iterator
import bson
from websockets.client import WebSocketClientProtocol, connect as ws_connect_async
@ -141,6 +141,20 @@ class WebSocket:
def __exit__(self, exc_type, exc_value, traceback):
self.close()
def __iter__(self) -> Iterator[WebSocketResponse]:
"""
Iterate on incoming messages.
The iterator calls :meth:`recv` and yields messages in an infinite loop.
It exits when the connection is closed normally. It raises a
:exc:`~websockets.exceptions.ConnectionClosedError` exception after a
protocol error or a network failure.
"""
for message in self.ws:
yield message
def send(self, data: WebSocketRequest):
"""Send data to the websocket."""
self.ws.send(json.dumps(data.to_dict()))