This example might help
It’s in nodeJS, but however demonstrates a userthing thing:
If I don’t get a Twitch sent PING in a defined time, I send my own PING for Twitch to PONG to.
This might help keep your connection alive.
Don’t know if your problem is a server connectivity issue or actually a problem with your code.