python-engineio icon indicating copy to clipboard operation
python-engineio copied to clipboard

HTTP session override may not work correctly

Open irtimir opened this issue 7 months ago • 3 comments

I use my custom HTTP session with overridden TCPConnector.

conn = aiohttp.TCPConnector(ssl=ssl_context)

socketio.AsyncClient(
    ...,
    http=aiohttp.ClientSession(connector=conn),
)

And after the reconnect is called, the default session can override the session I passed:

https://github.com/miguelgrinberg/python-engineio/blob/7a41390fcae7ff05b1b729168a7f1139f17e06a2/src/engineio/async_client.py#L308-L310

My suggestion is to create a separate method in the base class that contains the logic for creating a session.

Then you can do it like this:

# engineio.async_client.AsyncClient
class EIOCustomSession(engineio.AsyncClient):
    def create_session(self):
        return aiohttp.ClientSession()

    async def _connect_websocket(self, url, headers, engineio_path):
        ...
        if self.http is None or self.http.closed:  # pragma: no cover
            self.http = self.create_session()
        ...

# mymodule
class EIOCustomSession(engineio.AsyncClient):
    def __init__(self, *args, **kwargs, conn=None)
        self.conn = conn

    def create_session(self):
        return aiohttp.ClientSession(connector=self.conn)


class SIOCustomSession(socketio.AsyncClient):
    def _engineio_client_class(self):
        return EIOCustomSession

irtimir avatar May 28 '25 07:05 irtimir