一段时间后,TcpClient从Twitch IRC断开连接(TcpClient disconnects after awhile from Twitch IRC)

编程入门 行业动态 更新时间:2024-10-28 12:26:38
一段时间后,TcpClient从Twitch IRC断开连接(TcpClient disconnects after awhile from Twitch IRC)

我的问题是,在我连接到IRC并且我从IRC接收原始文本并记录它之后,它只是决定在不活动后或在我启动后2-3秒(未经证实)与服务器断开连接。

任何帮助,将不胜感激。 这是我的代码(有问题在这里发布): http : //pastebin.com/Ls5rv0RP

我需要它来停止断开,但我真的找不到办法。 我知道流行的mIRC客户端在x时间后与Twitch断开连接但重新连接,只要它知道及时重新连接(2-5秒)就可以了。

这部分是回复PING / PONG的:

if (buf.StartsWith("PING ")) output.Write(buf.Replace("PING", "PONG") + "\r\n"); output.Flush();

任何帮助表示赞赏。

My issue is that after I connect to the IRC and I am receiving raw text from the IRC and logging it but then it just decides to disconnect from the server after inactivity or 2-3 seconds after I start it (unconfirmed).

Any help would be appreciated. Here's my code (had issues posting it here): http://pastebin.com/Ls5rv0RP

I need it to stop disconnecting but I cant really find a way to. I know the popular mIRC client disconnects from Twitch after x amount of time but reconnects and that's fine to as long as it KNOWS to reconnect in a timely manner (2-5 seconds).

The part were it replys to PING/PONG's:

if (buf.StartsWith("PING ")) output.Write(buf.Replace("PING", "PONG") + "\r\n"); output.Flush();

Any help is appreciated.

最满意答案

下面的代码通过运行一个ping到IRC服务器的单独线程来帮助防止我的Twitch bot断开连接。

PingSender类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace TwitchBot { /* * Class that sends PING to irc server every 5 minutes */ class PingSender { static string PING = "PING "; private Thread pingSender; // Empty constructor makes instance of Thread public PingSender() { pingSender = new Thread (new ThreadStart (this.Run) ); } // Starts the thread public void Start() { pingSender.Start(); } // Send PING to irc server every 5 minutes public void Run() { while (true) { Program.irc.sendIrcMessage(PING + "irc.twitch.tv"); Thread.Sleep(300000); // 5 minutes } } } }

用法:

/* Ping to twitch server to prevent auto-disconnect */ PingSender ping = new PingSender(); ping.Start();

This code below helps keep my Twitch bot from disconnecting by running a separate thread that pings to the IRC server.

PingSender Class:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace TwitchBot { /* * Class that sends PING to irc server every 5 minutes */ class PingSender { static string PING = "PING "; private Thread pingSender; // Empty constructor makes instance of Thread public PingSender() { pingSender = new Thread (new ThreadStart (this.Run) ); } // Starts the thread public void Start() { pingSender.Start(); } // Send PING to irc server every 5 minutes public void Run() { while (true) { Program.irc.sendIrcMessage(PING + "irc.twitch.tv"); Thread.Sleep(300000); // 5 minutes } } } }

Usage:

/* Ping to twitch server to prevent auto-disconnect */ PingSender ping = new PingSender(); ping.Start();

更多推荐

本文发布于:2023-08-02 18:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379719.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Twitch   TcpClient   IRC   awhile   disconnects

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!