Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 27
[AIT-96] feat: add msgpack suppport for websocket transport#648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
53504cd4d57f967a2e01478d8233File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,6 +8,8 @@ | ||
| from enum import IntEnum | ||
| from typing import TYPE_CHECKING | ||
| import msgpack | ||
| from ably.http.httputils import HttpUtils | ||
| from ably.types.connectiondetails import ConnectionDetails | ||
| from ably.util.eventemitter import EventEmitter | ||
| @@ -71,6 +73,7 @@ def __init__(self, connection_manager: ConnectionManager, host: str, params: dic | ||
| self.is_disposed = False | ||
| self.host = host | ||
| self.params = params | ||
| self.format = params.get('format', 'json') | ||
| super().__init__() | ||
| def connect(self): | ||
| @@ -189,12 +192,23 @@ async def ws_read_loop(self): | ||
| raise AblyException('ws_read_loop started with no websocket', 500, 50000) | ||
| try: | ||
| async for raw in self.websocket: | ||
| msg = json.loads(raw) | ||
| task = asyncio.create_task(self.on_protocol_message(msg)) | ||
| task.add_done_callback(self.on_protcol_message_handled) | ||
| # Decode based on format | ||
| try: | ||
| msg = self.decode_raw_websocket_frame(raw) | ||
| task = asyncio.create_task(self.on_protocol_message(msg)) | ||
| task.add_done_callback(self.on_protcol_message_handled) | ||
| except Exception as e: | ||
| log.exception( | ||
| f"WebSocketTransport.decode(): Unexpected exception handling channel message: {e}" | ||
| ) | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| except ConnectionClosedOK: | ||
| return | ||
| def decode_raw_websocket_frame(self, raw: str | bytes) -> dict: | ||
| if self.format == 'msgpack': | ||
| return msgpack.unpackb(raw, raw=False) | ||
| return json.loads(raw) | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def on_protcol_message_handled(self, task): | ||
| try: | ||
| exception = task.exception() | ||
| @@ -231,8 +245,13 @@ async def close(self): | ||
| async def send(self, message: dict): | ||
| if self.websocket is None: | ||
| raise Exception() | ||
| raw_msg = json.dumps(message) | ||
| log.info(f'WebSocketTransport.send(): sending {raw_msg}') | ||
| # Encode based on format | ||
| if self.format == 'msgpack': | ||
| raw_msg = msgpack.packb(message, use_bin_type=True) | ||
| log.info(f'WebSocketTransport.send(): sending msgpack message (length: {len(raw_msg)} bytes)') | ||
| else: | ||
| raw_msg = json.dumps(message) | ||
| log.info(f'WebSocketTransport.send(): sending {raw_msg}') | ||
| await self.websocket.send(raw_msg) | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def set_idle_timer(self, timeout: float): | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.