Reputation: 111
I try to write my own bot in Python to connect and interact with the Twitch-chat via its IRC interface. I have the connection and can receive and send data. I also answer the incoming PINGs with PONG (they get send after around 5 minutes). But after about 3 minutes of inactivity (so before I even get a PING) the connection fails with
socket.error: [Errno 104] Connection reset by peer
I use the socket
module in Python for the connection. The error occurs when the recv()
method is called on my socket.
Some things I was able to figured out:
Before the connection fails, their is no incoming (unanswered) PING.
The time between the last chat message and the disconnect seams to be around 3 minutes every time. And its enough to receive something (linke a chat-message by someone else) to reset this timer. I don't have to send anything myself.
I first thought that I time out myself because I don't receive anything for to long. That doesn't seem to be the case, because if I set a lower timeout time for socket.recv()
I receive socket.timeout: timed out
, not the error shown above.
I think I don't miss any data send to me because all chat messages come through. But I post my main loop for receiving data below just to be sure.
My Question is now, why does the connection fail and how can I prevent this?
I have the feeling this is a vague question. It's the first time I try to work with an IRC interface and I don't rely know how to get more information on what exactly the problem is.
Just in case, here's the mains loop I use to receive data. I think this works fine because all the chat messages come through. But maybe there's a mistake and I miss some incoming data (like a PING).
readbuffer = ''
while True:
readbuffer = readbuffer + s.recv(1024)
temp = string.split(readbuffer, '\n')
readbuffer = temp.pop()
for line in temp:
print(line)
# PING/PONG
if "PING :tmi.twitch.tv" in line:
print("PONG :tmi.twitch.tv")
s.send(line.replace('PING', 'PONG'))
Here s
is a socket()
form the socket
module.
Upvotes: 1
Views: 1337
Reputation: 111
Pinging the server myself every 2 minutes solved the problem. I just added this function
import time, threading
def sendPing(s):
print('sending PING')
s.send("PING :tmi.twitch.tv")
threading.Timer(120, sendPing, [s]).start()
sendPing(s)
before the main loop.
Thanks to ChatterOne for the tip.
Upvotes: 3