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,4 +1,4 @@
from typing import Any, Dict, Optional, Union, List
from typing import Iterator, Any, Dict, Optional, Union, List
import json
import bson
@ -167,6 +167,20 @@ class WebSocket:
def __exit__(self, exc_type, exc_value, traceback):
self.close()
def __iter__(self) -> Iterator[{{response_type}}]:
"""
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:{% for arg in args %}{%if arg.name == "body" %}{{arg.type}}{% endif %}{% endfor %}):
"""Send data to the websocket."""
self.ws.send(json.dumps(data.to_dict()))