java irc bot在11分钟后断开连接

时间:2016-08-10 12:30:47

标签: java irc

我正在尝试为我的抽搐频道创建一个机器人,它似乎正在连接好并发布读/写精细
问题是应用似乎在11分钟后死亡 任何想法我需要做什么才能永远保持联系? (“我们必须回应PING以避免断开连接。” 我没有错误只是建立成功(总时间:11分0秒) 好像在工作)

public class Twchatbot {

/**
 * @param args the command line arguments
 */
    // TODO code application logic here
    public static void main(String[] args) throws Exception {
    long cur=System.currentTimeMillis();
    int i=0;
    String str[]={"XD","hi:)","cool"};   

    // The server to connect to and our details.
    String server = "irc.chat.twitch.tv";
    String nickname = "********";
    String password = "*******************";

    // The channel which the bot will join.
    String channel = "#********";

    // Connect directly to the IRC server.
    Socket socket = new Socket(server, 6667);
    BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream( )));
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(socket.getInputStream( )));

    // Log on to the server.
    writer.write("pass " + password + "\r\n");
    writer.write("nick " + nickname + "\r\n");

    writer.flush( );

    // Read lines from the server until it tells us we have connected.
    String line = null;
    while ((line = reader.readLine( )) != null) {
        System.out.println(line);
        if (line.indexOf("004") >= 0) {
            // We are now logged in.
            break;
        }
        else if (line.indexOf("433") >= 0) {
            System.out.println("Nickname is already in use.");
            return;
        }
    }

    // Join the channel.
    writer.write("JOIN " + channel + "\r\n");
    writer.flush( );

    // Keep reading lines from the server.

    while ((line = reader.readLine( )) != null) {
        if (line.toLowerCase( ).startsWith("PING ")) {
            // We must respond to PINGs to avoid being disconnected.
            writer.write("PONG " + line.substring(5) + "\r\n");
            writer.write("PRIVMSG " + channel + " :I got pinged!\r\n");
            writer.flush( );


             if(System.currentTimeMillis()-cur>600000)  
             {
                 writer.write("PRIVMSG "+ channel +" :"+str[i]+"\r\n");
                 writer.flush();

                 i++;
                 if(i>=3)
                     i=0;
                 cur=System.currentTimeMillis();
             }

        }
        else {
            // Print the raw line received by the bot.

             if(System.currentTimeMillis()-cur>600000)  
             {
                 writer.write("PRIVMSG "+ channel +" :"+str[i]+"\r\n");
                 writer.flush();

                 i++;
                 if(i>=3)
                     i=0;
                 cur=System.currentTimeMillis();
             }
            if(line.contains("!enter"))
            {
                writer.write("PRIVMSG "+ channel +" :"+str[i]+"\r\n");
                writer.flush();
            }
            System.out.println(line);
        }
    }
}
}

欢迎任何提示:)
thx in advs 为不好的英语而烦恼。

1 个答案:

答案 0 :(得分:1)

if (line.toLowerCase( ).startsWith("PING ")) {

想想那是做什么的。它将行更改为小写,然后检查它是否以大写中的PING开头。

使用调试器并设置断点以查看发生了什么以及没有发生什么。像这样的错误变得很明显。