bynect / pytmi

TMI (Twitch Messaging Interface) library for Python
https://pypi.org/project/pytmi/
MIT License
9 stars 0 forks source link

[Q] What is `part` method? #2

Closed ZaneH closed 1 year ago

ZaneH commented 1 year ago

I'm facing an issue that only occurs after the first connection with pytmi. I see:

Unknown exception 0 bytes read on a total of undefined expected bytes

in my console on subsequent requests. My run loop generally looks like this:

@router.websocket("/chat")
async def scan_chat(websocket: WebSocket, channel: str = ''):
    await websocket.accept()

    twitch_client = pytmi.Client(use_ssl=False)

    try:
        await twitch_client.login_anonymous()
        await twitch_client.join(channel)
    except Exception as e:
        print("Login failed: {}".format(e))
        pass

    async def get_message_loop():
        msg = await twitch_client.get_message()
        if msg is None or not msg.valid or not "PRIVMSG" in msg.command:
            return

        privmsg = msg.command.split(" :", 1)[1]
        sender = msg.tags.get('display-name', '')
        await websocket.send_text(privmsg)

        del msg

    try:
        while True:
            await get_message_loop()
    except ConnectionClosedOK:
        pass
    except ConnectionClosedError as e:
        print("Twitch: ConnectionClosedError {}".format(e))
        pass
    except Exception as e:
        print("Unknown exception {}".format(e))
        pass
    finally:
        await twitch_client.logout()
        await websocket.close()
        print('Disconnected a Twitch client for {}'.format(channel))

I'm looking for a method to close my connections properly. I saw part in the example code near the end. Will that be helpful for resolving this?

I'm not sure if these are relevant but I see these as well:

Login failed: Unable to complete login
Tried 8 times, got 8 errors:
readuntil() called while another coroutine is already waiting for incoming data

unable to perform operation on <TCPTransport closed=True reading=False 0x7fd51f75cf90>; the handler is closed
unable to perform operation on <TCPTransport closed=True reading=False 0x7fd51f75cf90>; the handler is closed
unable to perform operation on <TCPTransport closed=True reading=False 0x7fd51f75cf90>; the handler is closed
unable to perform operation on <TCPTransport closed=True reading=False 0x7fd51f75cf90>; the handler is closed
unable to perform operation on <TCPTransport closed=True reading=False 0x7fd51f75cf90>; the handler is closed
unable to perform operation on <TCPTransport closed=True reading=False 0x7fd51f75cf90>; the handler is closed

Unknown exception Not logged in

...
Login failed: [Errno 54] Connection reset by peer
ZaneH commented 1 year ago

I think the error came from trying get_message() without logging in.

I added:

twitch_client = pytmi.Client(use_ssl=False)
async def get_message_loop():
    while not twitch_client.joined:
        await twitch_client.login_anonymous()
        await twitch_client.join(channel)
...

a simple check for .joined and it seems to work nicely with the existing loop. I didn't need to change anything else.

bynect commented 1 year ago

Hello, the part method is used for leaving a channel (that you previously joined with join). Anyway, the error you got seems to be a spurious error resulting from a connection error. Sometimes I also get a similar error, I'll try to figure out a way to fix it hopefully.