【发布时间】:2015-03-27 18:05:07
【问题描述】:
我正在使用ircdotnet 库,我正在尝试在超时时重新连接,但是当我在 IRC 中超时时,客户端无法识别它已断开连接,我的代码有什么问题,我该如何重新连接什么时候超时?
namespace IRCBOT
{
class Program
{
public IrcClient zIrcClient = new IrcClient();
static void Main(string[] args)
{
Connect();
}
public static void Connect()
{
Program p = new Program("irc.server.net", new IrcUserRegistrationInfo()
{
NickName = "justatest",
UserName = "test",
RealName = "test",
Password = ""
});
}
public Program(string host, IrcRegistrationInfo info)
{
try
{
zIrcClient.Connect(host, 6667, false, info);
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + "Connecting");
zIrcClient.Connected += ircClient_Connected;
zIrcClient.Registered += ircClient_Registered;
while (true)
{
if (!zIrcClient.IsConnected)
{
zIrcClient.Disconnected += ircClient_Disconnected;
}
string text = Console.ReadLine();
if (text == "/quit")
{
zIrcClient.Quit();
break;
}
else
{
zIrcClient.LocalUser.SendMessage("#testchamber", text);
}
}
Console.ReadKey();
zIrcClient.Dispose();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.ReadKey();
}
}
private void ircClient_Connected(object sender, EventArgs e)
{
try
{
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + "Connected");
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
Console.ReadKey();
}
}
private void ircClient_Disconnected(object sender, EventArgs e)
{
try
{
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + "Disconnected");
zIrcClient.Connected -= ircClient_Connected;
zIrcClient.Registered -= ircClient_Registered;
zIrcClient.Disconnected -= ircClient_Disconnected;
zIrcClient.Dispose();
Connect();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.ReadKey();
}
}
}
private void ircClient_Registered(object sender, EventArgs e)
{
zIrcClient.Channels.Join("#testchamber");
}
}
【问题讨论】: