racinette / websockets_proxy

Connect to websockets through a proxy server.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

proxy_connect does not work outside context manager

Eli-Chandler opened this issue · comments

    async def start(self):
        self._connection_internal = await proxy_connect(
            'wss://example.com',
            ping_interval=None,
            origin=websockets.Origin('https://example.com'),
            proxy=self._proxy
        )

        self.is_connected = True

        self._receive_task = asyncio.create_task(self._receive())
Traceback (most recent call last):
  File "/Users/eli/PycharmProjects/KrunkerAPIV2/example.py", line 25, in <module>
    asyncio.run(main())
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/eli/PycharmProjects/KrunkerAPIV2/example.py", line 16, in main
    item = await api.get_item('609')
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eli/PycharmProjects/KrunkerAPIV2/src/krunker_apis/adapters/rotating_api.py", line 59, in get_item
    return await api.get_item(id)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eli/PycharmProjects/KrunkerAPIV2/src/krunker_apis/adapters/websocket_api.py", line 71, in get_item
    ws = await self._get_websocket()
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/eli/PycharmProjects/KrunkerAPIV2/src/krunker_apis/adapters/websocket_api.py", line 38, in _get_websocket
    await self._websocket.start()
  File "/Users/eli/PycharmProjects/KrunkerAPIV2/src/websocket.py", line 29, in start
    self._connection_internal = await proxy_connect(
                                ^^^^^^^^^^^^^^^^^^^^
  File "/Users/eli/PycharmProjects/KrunkerAPIV2/venv/lib/python3.11/site-packages/websockets/legacy/client.py", line 646, in __await_impl_timeout__
    async with asyncio_timeout(self.open_timeout):
                               ^^^^^^^^^^^^^^^^^
AttributeError: 'ProxyConnect' object has no attribute 'open_timeout'

This works fine with the defaults websockets library

Ended up doing this instead: (is_connected is now an asyncio.Event)

    async def start(self):
        # self._connection_internal = await proxy_connect(
        #     'wss://social.krunker.io/ws?',
        #     ping_interval=None,
        #     origin=websockets.Origin('https://krunker.io'),
        #     proxy=self._proxy
        # )
        #
        # self.is_connected = True

        self._receive_task = asyncio.create_task(self._receive())
        await self.is_connected.wait()
    async def _receive(self):
        async with proxy_connect(
            'wss://social.krunker.io/ws?',
            ping_interval=None,
            origin=websockets.Origin('https://krunker.io'),
            proxy=self._proxy
        ) as ws:
            logging.info('Connected to websocket')
            self._connection_internal = ws
            self.is_connected.set()

            while True:
                message = decode_message(await self._connection.recv())
                logging.info(f'Received: {message}')

                asyncio.create_task(self.handle_receive(message))

fixed in 0.1.2