簡體   English   中英

TCP客戶端未連接到IRC地址

[英]TCP Client is not Connecting to IRC Adress

我正在嘗試建立一個抽動聊天機器人。 我已經閱讀了一些教程,到目前為止,這是我編寫的代碼:

namespace IRC_Client
{
    class Program
    {
        public NetworkStream NetStream = null;
        public StreamReader StreamRead = null;
        public StreamWriter StreamWrite = null;

        public System.Net.Sockets.TcpClient Socket_TcpClient;

        static void Main(string[] args)
        {
            Program P = new Program();
            P.IRC_Connect();

            Console.Read();
        }

        public void IRC_Connect()
        {
            int port;
            string  nick, owner, server, oauth;

            nick = "xxx";
            Console.WriteLine("Bot Name: {0}", nick);
            oauth = "oauth:xxxxxxxxxxxxxxxxxxx";
            Console.WriteLine("Password Token: {0}", oauth);
            owner = "xxx";
            Console.WriteLine("Bot owner name: {0}", owner);
            server = "irc.twitch.tv";
            Console.WriteLine("Server name: {0}", server);                   
            port = 6667;
            Console.WriteLine("Portnummber: {0} ", port);

            try
            {
                Socket_TcpClient = new TcpClient(server, port);
            }
            catch
            {
                Console.WriteLine("Failed to connect!");
                return;
            }

            Console.WriteLine("Connected");

            IRCWork();

            try
            {
                NetStream = Socket_TcpClient.GetStream();
                StreamRead = new StreamReader(NetStream);
                StreamWrite = new StreamWriter(NetStream);
                SendData("PASS", oauth);
                SendData("NICK", nick);
            }
            catch
            {
                Console.WriteLine("Communication error");
                throw;
            }
        }

        public void SendData(string cmd, string param)
        {
            if (param == null)
            {
                StreamWrite.WriteLine(cmd);
                StreamWrite.Flush();
                Console.WriteLine(cmd);
            }
            else
            {
                StreamWrite.WriteLine(cmd + " " + param);
                StreamWrite.Flush();
                Console.WriteLine(cmd + " " + param);
            }
        }

        public void IRCWork()
        {
            string[] Text;
            string data;
            bool shouldRun = true;
            while (shouldRun)
            {
                data = StreamRead.ReadLine();
                Console.WriteLine(data);
                char[] charSeparator = new char[] { ' ' };
                Text = data.Split(charSeparator, 5);

                if (Text[0] == "PING")
                {
                    SendData("PONG", Text[1]);
                }

                if (Text.Length > 4) //is the command received long enough to be a bot command?
                {
                    string command = Text[3]; //grab the command sent

                    switch (command)
                    {
                        case ":!join":
                            SendData("JOIN", Text[4]);
                            //if the command is !join send the "JOIN" command to the server with the parameters set by the user
                            break;
                        case ":!say":
                            SendData("PRIVMSG", Text[2] + " " + Text[4]);
                            //if the command is !say, send a message to the chan (ex[2]) followed by the actual message (ex[4]).
                            break;
                        case ":!quit":
                            SendData("QUIT", Text[4]);
                            //if the command is quit, send the QUIT command to the server with a quit message
                            shouldRun = false;
                            //turn shouldRun to false - the server will stop sending us data so trying to read it will not work and result in an error. This stops the loop from running and we will close off the connections properly
                            break;
                    }
                }
            }
        }

        public void Dispose()
        {
            if (StreamRead != null)
                StreamRead.Close();
            if (StreamWrite != null)
                StreamWrite.Close();
            if (NetStream!= null)
                NetStream.Close();
            if (Socket_TcpClient != null)
                Socket_TcpClient.Close();
        }
    }
}

主要問題是連接部分。 我還遠遠不夠測試其余部分。 服務器地址和端口應該正確,但是程序只是在這里停止,什么也沒有發生。 (我已經使用了來自此站點的信息: http : //help.twitch.tv/customer/com/portal/articles/1302780-twitch-irc

try
{
    Socket_TcpClient = new TcpClient(server, port);
}

我希望有人知道我的問題的答案。

我還沒有機會測試您的代碼,但是我已經為Twitch Bot建立了此類

IrcClient.cs

class IrcClient
{
    public string userName;
    private string channel;

    private TcpClient tcpClient;
    private StreamReader inputStream;
    private StreamWriter outputStream;

    public IrcClient(string ip, int port, string userName, string password)
    {
        this.userName = userName;

        tcpClient = new TcpClient(ip, port);
        inputStream = new StreamReader(tcpClient.GetStream());
        outputStream = new StreamWriter(tcpClient.GetStream());

        outputStream.WriteLine("PASS " + password);
        outputStream.WriteLine("NICK " + userName);
        outputStream.WriteLine("USER " + userName + " 8 * :" + userName);
        outputStream.Flush();
    }

    public void joinRoom(string channel)
    {
        this.channel = channel;
        outputStream.WriteLine("JOIN #" + channel);
        outputStream.Flush();
    }
}

用法

// Password from www.twitchapps.com/tmi/
// include the "oauth:" portion
/* main server: irc.twitch.tv, 6667 */
string oauth = "oauth:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
irc = new IrcClient("irc.twitch.tv", 6667, "BOT_NAME", oauth);
irc.joinRoom("CHANNEL_NAME");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM