centrifugal / centrifugal/centrifuge-python
Passing sock parameter to websockets library
- Dominant language
- Python
- Stars
- 61
- Forks
- 19
- Avg merge
- 7h 26m
- Merged PRs (30d)
- 12
Description
Hi,
I'm using this library on a server with multiple public IP addresses and I need to be able to specify an exact IP address for `websockets` library to use. I checked library codes and found where it calls the `connect` function of `websockets` library:
```python
async def _create_connection(self) -> bool:
if self.state != ClientState.CONNECTING:
return False
subprotocols = []
if self._use_protobuf:
subprotocols = ["centrifuge-protobuf"]
try:
self._conn = await websockets.connect(
self._address,
subprotocols=subprotocols,
extra_headers=self._headers,
)
except (OSError, exceptions.WebSocketException) as e:
handler = self.events.on_error
await handler(ErrorContext(code=_code_number(_ErrorCode.TRANSPORT_CLOSED), error=e))
asyncio.ensure_future(self._schedule_reconnect())
return False
```
To specify a certain IP address for `websockets` library, I need to pass `sock` parameter to the `websockets.connect` function, like this:
```python
async def _create_connection(self) -> bool:
if self.state != ClientState.CONNECTING:
return False
subprotocols = []
if self._use_protobuf:
subprotocols = ["centrifuge-protobuf"]
try:
self._conn = await websockets.connect(
self._address,
subprotocols=subprotocols,
extra_headers=self._headers,
sock=my_tcp_socket
)
except (OSError, exceptions.WebSocketException) as e:
handler = self.events.on_error
await handler(ErrorContext(code=_code_number(_ErrorCode.TRANSPORT_CLOSED), error=e))
asyncio.ensure_future(self._schedule_reconnect())
return False
```
So if it's possible, please add a `sock` argument to `Client` class and then pass it to `websockets.connect` function in `_create_connection` function.
Thanks for developing this library.
Contributor guide
No contributing guide indexed for this repository
Research direction
Locate the Client class and its _create_connection method, which calls websockets.connect with the address, subprotocols, and headers shown in the issue. Trace how Client options are initialized and used, then verify that a supplied sock reaches websockets.connect and that existing connection behavior remains unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- networking
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100