所以我正在制作一个连接到颤抖的irc聊天的机器人。这是代码:
Console.WriteLine("Joining Room");
IrcClient irc = new IrcClient("irc.twitch.tv", 6667, "ScottBots", "oauth:asdasd");
irc.joinRoom("ScottBots");
Console.WriteLine("Joined Room");
int i = 0;
while (true)
{
string message = irc.readMessage();
if(message.Contains("!helps"))
{
irc.sendChatMessage("Welcome to ScottBots! Currently in development.");
}
Console.WriteLine("loop: " + i);
i++;
}
看看真正的循环......一切都好吗?
现在看一下这个控制台应用程序给我的内容:
Joining Room
Joined Room
loop: 0
loop: 1
loop: 2
loop: 3
loop: 4
loop: 5
loop: 6
loop: 7
loop: 8
loop: 9
它只停在9?
许多人一直在问我的ircClient代码:
private string username;
private string channel;
private TcpClient tcpClient;
private StreamReader inputStream;
private StreamWriter outputSteam;
public IrcClient(string ip, int port, string username, string password)
{
this.username = username;
tcpClient = new TcpClient(ip, port);
inputStream = new StreamReader(tcpClient.GetStream());
outputSteam = new StreamWriter(tcpClient.GetStream());
try
{
outputSteam.WriteLine("PASS " + password);
outputSteam.WriteLine("NICK " + username);
outputSteam.WriteLine("USER " + username + " 8 * :" + username);
outputSteam.Flush();
} catch (Exception e)
{
}
}
public void joinRoom(string channel)
{
try
{
this.channel = channel;
outputSteam.WriteLine("JOIN #" + channel);
outputSteam.Flush();
}
catch (Exception e)
{
Console.WriteLine("Failed to join room");
}
}
public void sendIrcMessage(string message)
{
try
{
outputSteam.WriteLine(message);
outputSteam.Flush();
}
catch (Exception e)
{
Console.WriteLine("failed to run sendIrcMessage() method");
}
}
public void sendChatMessage(string message)
{
sendIrcMessage(":" + username + "!" + username + "@" + username + ".tmi.twitch.tv PRIVNSG #" + channel + " : " + message);
}
public string readMessage()
{
string message = inputStream.ReadLine();
return message;
}
答案 0 :(得分:3)
看起来irc.SendChatMessage()
或irc.ReadMessage()
中的一个是blocking,也许当它用完缓冲的输入/输出并需要在套接字上等待时。
编辑:几乎可以肯定,irc.ReadMessage()
电话正在阻止。它会针对Stream调用ReadLine()
,而Stream又与TcpClient
相关联。如果要构建IRC bot和测试装备,您可能希望查看线程和/或异步回调。以下是一个示例(与IRC无关):http://sunildube.blogspot.ca/2011/12/asynchronous-tcp-client-easy-example.html