【问题标题】:While true loop not infinite?虽然真正的循环不是无限的?
【发布时间】:2015-08-11 20:35:29
【问题描述】:

所以我正在制作一个连接到 twitch 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++;
}

看看 while true 循环......一切都好吗?

现在看看这个控制台应用程序给了我什么:

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;
    }

【问题讨论】:

  • 很可能您的 irc.XXX 方法之一失败并在循环中引发异常。
  • 我应该尝试一下吗? @Eser
  • string message = irc.readMessage(); 这是否等待消息,例如Console.ReadLine()?也许它正在等待输入。当程序看似停止时暂停,然后调试它
  • irc 是否有一个 API,一旦有消息可用,您就会在其中进行回调?这对您的资源来说会容易得多...
  • @flp 我不这么认为。我是 C# 的新手,不知道该怎么做。对不起。我用我的 IrcClient 类更新了帖子。也许你可以看看。

标签: c# loops while-loop


【解决方案1】:

看起来irc.SendChatMessage()irc.ReadMessage() 之一是blocking,可能是当它用完缓冲的输入/输出并需要在套接字上等待时。

编辑:几乎可以肯定irc.ReadMessage() 调用被阻塞了。它针对流调用ReadLine(),而流又链接到TcpClient。如果你想构建一个 IRC 机器人和测试平台,你可能想研究线程和/或异步回调。这是一个示例(与 IRC 无关):http://sunildube.blogspot.ca/2011/12/asynchronous-tcp-client-easy-example.html

【讨论】:

  • 好的。我是 C# 新手,不太了解。如何缓冲并等待套接字?请帮忙。谢谢(更新了带有 IrcClient 类的帖子)
  • @CooperScott 用一个简单示例的链接更新了我的答案。
  • 我明白了,谢谢。看着代码。我不知道如何实施。说你回答了,因为它可能是答案,我可能只是愚蠢。如果你感觉不错。你能再帮我一些吗?大声笑,感谢到目前为止所做的一切!
  • @CooperScott 我认为最好按照自己的节奏完成一些关于在 C# 中构建客户端-服务器套接字代码的在线教程。谷歌一些通用的东西——即使它与 IRC 无关,花时间学习一些基本背景也会对你有好处。不幸的是,目前我没有时间做更多的事情,只能回答关于 Stack Overflow 的偶尔问题。
  • 谢谢你给我的所有帮助。了解您也在制定一个时间框架。再次感谢所有帮助和提示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 2011-12-03
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多