Kucoin / kucoin-python-sdk

MIT License
46 stars 16 forks source link

multiple errors with websocket #104

Closed itayl2 closed 2 months ago

itayl2 commented 1 year ago

Hi,

I love this package, thank you so much.

I am running into two issues with the websocket connection. This is my code:

async def main(loop):
    async def deal_msg(msg):
        if msg['topic'] == '/market/ticker:all':
            my_ticker_callback(Platform.KUCOIN, [msg])

    client = WsToken()
    ws_client = await KucoinWsClient.create(loop, client, deal_msg, private=False)
    await ws_client.subscribe('/market/ticker:all')
    while True:
        await asyncio.sleep(30)

def handler():
    while True:
        try:
            loop = asyncio.get_event_loop()
            loop.run_until_complete(main(loop))
        except Exception as e:
            logger.exception(f"Websocket failed")
            sleep(0.1)

When I use the code above, I run into this error every short while:

<Task finished name='Task-4' coro=<ConnectWebsocket._run() done, defined at ...\site-packages\kucoin\websocket\websocket.py:33> exception=ConnectionClosedError(None, None, None)> got an exception no close frame received or sent
_reconnect over.

When I change the sleep to 30, I get this every short while:

<Task finished name='Task-4' coro=<ConnectWebsocket._run() done, defined at ...\site-packages\kucoin\websocket\websocket.py:33> exception=ConnectionClosedError(None, None, None)> got an exception sent 1011 (unexpected error) keepalive ping timeout; no close frame received
_reconnect over.

Any idea how this can be solved?

quantokenizer commented 10 months ago

You don't need the second while True loop. You can put that whole try-except block under if __name__ == '__main__' and run it like that.

ISAAC-XXYYZZ commented 2 months ago

Because run_until_complete is a method that runs the event loop until a Future is done and returns the Future's result or raises its exception, you don't need to add a while True loop. Please refer to this and modify your code accordingly.

example_default_ws_public.py