Skip to content

Commit 0ffaef5

Browse files
DefiDebaucheryfselmo
authored andcommitted
Add flag to always convert websocket payloads to text frames
1 parent 4708938 commit 0ffaef5

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

docs/providers.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,16 @@ AsyncIPCProvider
291291
WebSocketProvider
292292
+++++++++++++++++
293293

294-
.. py:class:: web3.providers.persistent.WebSocketProvider(endpoint_uri: str, websocket_kwargs: Dict[str, Any] = {})
294+
.. py:class:: web3.providers.persistent.WebSocketProvider(endpoint_uri: str, websocket_kwargs: Dict[str, Any] = {}, use_text_frames: bool = False)
295295
296296
This provider handles interactions with an WS or WSS based JSON-RPC server.
297297

298298
* ``endpoint_uri`` should be the full URI to the RPC endpoint such as
299299
``'ws://localhost:8546'``.
300300
* ``websocket_kwargs`` this should be a dictionary of keyword arguments which
301301
will be passed onto the ws/wss websocket connection.
302+
* ``use_text_frames`` will ensure websocket data is sent as text frames
303+
for servers that do not support binary communication.
302304

303305
This provider inherits from the
304306
:class:`~web3.providers.persistent.PersistentConnectionProvider` class. Refer to

newsfragments/3619.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `use_text_frames` flag for `WebSocketProvider` to work around websocket servers that don't support binary frames

web3/providers/persistent/websocket.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def __init__(
6363
self,
6464
endpoint_uri: Optional[Union[URI, str]] = None,
6565
websocket_kwargs: Optional[Dict[str, Any]] = None,
66+
use_text_frames: Optional[bool] = False,
6667
# `PersistentConnectionProvider` kwargs can be passed through
6768
**kwargs: Any,
6869
) -> None:
@@ -71,6 +72,7 @@ def __init__(
7172
URI(endpoint_uri) if endpoint_uri is not None else get_default_endpoint()
7273
)
7374
super().__init__(**kwargs)
75+
self.use_text_frames = use_text_frames
7476
self._ws: Optional[WebSocketClientProtocol] = None
7577

7678
if not any(
@@ -118,6 +120,9 @@ async def socket_send(self, request_data: bytes) -> None:
118120
"Connection to websocket has not been initiated for the provider."
119121
)
120122

123+
if self.use_text_frames:
124+
request_data = request_data.decode("utf-8")
125+
121126
await asyncio.wait_for(
122127
self._ws.send(request_data), timeout=self.request_timeout
123128
)

0 commit comments

Comments
 (0)