C# Twitch API #PRIVMSG 不工作并在一段时间后停止阅读聊天

问题描述

我能够连接到频道并从聊天中读取消息以处理命令,但一段时间后它停止读取消息并且不写入聊天。

当我尝试写入聊天时,它报告该消息不是有效命令。例如,如果要写入的消息是“Hello World”,那么它说“HELLO WORLD”不是一个有效的命令。我也能够很好地阅读聊天内容,但过了一会儿,尽管仍然连接到 Twitch 服务器,但它还是停止了阅读消息。在它停止之前,它确实通过了一些“乒乓”测试。代码如下:

*注意:定时器每秒调用一次 ReadChat() 函数

static void ConnectToTwitch()
{
    ConWin.UpdateTwitchLog("Twitch Client: Connecting...");
    TwitchClient = new TcpClient("irc.chat.twitch.tv",6667);
    SReader = new StreamReader(TwitchClient.GetStream());
    SWriter = new StreamWriter(TwitchClient.GetStream());
    SWriter.WriteLine("PASS " + AuthKey);
    SWriter.WriteLine("NICK " + UserName);
    SWriter.WriteLine("USER " + UserName + " 8 * :" + UserName);
    SWriter.WriteLine("JOIN #" + displayName);
    SWriter.Flush();
    string Response = SReader.ReadLine();
    if (Response.Contains("Welcome,GLHF"))
    {
        ConWin.UpdateTwitchLog("Twitch Client: Connected");
    }
    else
    {
        ConWin.UpdateTwitchLog("Twitch Client: Failed to Connect");
        Debug.Log("Twitch - ConnectToTwitch() -> Failed to Connect",3);
        return;
    }
    ConWin.UpdateTwitchLog(Response);
    string Response2 = SReader.ReadLine();
    ConWin.UpdateTwitchLog(Response2);
}

public static void Writetochat(string Msg)
{
    SWriter.WriteLine("PRIVMSG #" + displayName + " :" + Msg);// + "\r\n");
    //SWriter.WriteLine(string.Format(":" + UserName + "!" + UserName + "@" + UserName + ".tmi.twitch.tv PRIVMSG #" + displayName + " :" + Msg));
    //SWriter.WriteLine("PRIVMSG #" + displayName + " :" + Msg + ":" + UserName + "!" + UserName + "@" + UserName + ".tmi.twitch.tv PRIVMSG #" + displayName + " :" + Msg);// + "\r\n");
    SWriter.Flush();
}

static void ReadChat()
{
    Count++;
    if (!TwitchClient.Connected)
    {
        ConnectToTwitch();
        return;
    }
    if(TwitchClient.Available > 0)
    {
        var Msg = SReader.ReadLine();
        if (Msg.Contains("PING") && Msg.Contains("tmi.twitch.tv"))
        {
            SWriter.WriteLine("PONG :tmi.twitch.tv");
            Msg += " -> PONG :tmi.twitch.tv";
        }
        else if (Msg.Contains("PRIVMSG"))
        {
            var splitPoint = Msg.IndexOf("!",1);
            var ChatName = Msg.Substring(0,splitPoint);
            ChatName = ChatName.Substring(1);
            splitPoint = Msg.IndexOf(":",1);
            Msg = Msg.Substring(splitPoint + 1);
            if (Msg.Substring(0,1) == "!")
            {
                Msg = Master.ProcessInput(Msg.ToLower(),ChatName);
                Msg = ChatName + ": " + Msg;
            }
            else
                Msg = "";
            //if (Msg.Substring(0,1) == "!" && ChatName == displayName.ToLower())
            //    StreamerChatCommands(Msg);
        }
        if(Msg != "")
            ConWin.UpdateTwitchLog(Msg);
    }
    ChatWriterTimer -= 1;
    if (ChatWriterTimer <= 0)
    {
        Writetochat("Test");
        //Writetochat("Type '!' and the number of the option you wish to Vote for!");
        ChatWriterTimer = 10;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)