Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q] What is part method? #2

Closed
ZaneH opened this issue Jan 9, 2023 · 2 comments
Closed

[Q] What is part method? #2

ZaneH opened this issue Jan 9, 2023 · 2 comments

Comments

@ZaneH
Copy link

ZaneH commented Jan 9, 2023

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
Copy link
Author

ZaneH commented Jan 9, 2023

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.

@ZaneH ZaneH closed this as completed Jan 9, 2023
@bynect
Copy link
Owner

bynect commented Jan 11, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants