java irc 机器人在 11 分钟后断开连接
java irc bot disconnect after 11mints
我正在尝试为我的 twitch 频道创建一个机器人,它似乎连接正常并发布 read/write 很好
问题是应用程序似乎每次都在 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);
}
}
}
}
欢迎任何提示:)
感谢广告
抱歉英语不好。
if (line.toLowerCase( ).startsWith("PING ")) {
想一想它的作用。它将行更改为 lowercase 然后检查它是否以 uppercase.
开头的 PING
使用调试器并设置断点,看看会发生什么,不会发生什么。像这样的错误变得很明显。
我正在尝试为我的 twitch 频道创建一个机器人,它似乎连接正常并发布 read/write 很好
问题是应用程序似乎每次都在 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);
}
}
}
}
欢迎任何提示:)
感谢广告
抱歉英语不好。
if (line.toLowerCase( ).startsWith("PING ")) {
想一想它的作用。它将行更改为 lowercase 然后检查它是否以 uppercase.
开头的 PING使用调试器并设置断点,看看会发生什么,不会发生什么。像这样的错误变得很明显。