I created a custom tweak bot using the cavariux library. Called methods in the main class in turn.
bot.setOauth_Key("oauth:key_Value"); bot.connect(); bot.joinChannel(channel.toString()); bot.start(); Approximately one of the 5-6 launches of the bot is accompanied by an error java.net.SocketException: Connection reset by peer. The stack trace indicates that the error starts on this line.
while ((line = this.reader.readLine( )) != null && !stopped) TwitchBot class in the start () method. In no way did this library change except for adding encoding to the connect method (String ip, int port)
this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8)); this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); Testing some people on the computer of his bot, some error occurs more often. The start () method of the TwitchBоt class.
public final void start() { if (isRunning()) return; String line = ""; stopped = false; try { while ((line = this.reader.readLine( )) != null && !stopped) { if (line.toLowerCase( ).startsWith("ping")) { LOGGER.log(Level.INFO,"> PING"); LOGGER.log(Level.INFO,"< PONG " + line.substring(5)); this.writer.write("PONG " + line.substring(5) + "\r\n"); this.writer.flush(); } else if (line.contains("PRIVMSG")) { String str[]; str = line.split("!"); final User msg_user = User.getUser(str[0].substring(1, str[0].length())); str = line.split(" "); Channel msg_channel; msg_channel = Channel.getChannel(str[2], this); String msg_msg = line.substring((str[0].length() + str[1].length() + str[2].length() + 4), line.length()); LOGGER.log(Level.INFO,"> " + msg_channel + " | " + msg_user + " >> " + msg_msg); if (msg_msg.startsWith(commandTrigger)) onCommand(msg_user, msg_channel, msg_msg.substring(1)); if (msg_user.toString().equals("jtv") && msg_msg.contains("now hosting")) { String hoster = msg_msg.split(" ")[0]; onHost(User.getUser(hoster), msg_channel); } onMessage(msg_user, msg_channel, msg_msg); } else if (line.contains(" JOIN ")) { String[] p = line.split(" "); String[] pd = line.split("!"); if (p[1].equals("JOIN")) userJoins(User.getUser(pd[0].substring(1)), Channel.getChannel(p[2], this)); } else if (line.contains(" PART ")) { String[] p = line.split(" "); String[] pd = line.split("!"); if (p[1].equals("PART")) userParts(User.getUser(pd[0].substring(1)), Channel.getChannel(p[2], this)); } else if (line.contains(" WHISPER ")) { String[] parts = line.split(":"); final User wsp_user = User.getUser(parts[1].split("!")[0]); String message = parts[2]; onWhisper(wsp_user, message); } else if (line.startsWith(":tmi.twitch.tv ROOMSTATE")) { } else if (line.startsWith(":tmi.twitch.tv NOTICE")) { String[] parts = line.split(" "); if (line.contains("This room is now in slow mode. You may send messages every")) { LOGGER.log(Level.INFO,"> Chat is now in slow mode. You can send messages every " + parts[15] + " sec(s)!"); } else if (line.contains("subscribers-only mode")) { if (line.contains("This room is no longer")) LOGGER.log(Level.INFO,"> The room is no longer Subscribers Only!"); else LOGGER.log(Level.INFO,"> The room has been set to Subscribers Only!"); } else { LOGGER.log(Level.INFO,line); } } else if (line.startsWith(":jtv MODE ")) { String[] p = line.split(" "); if (p[3].equals("+o")) { LOGGER.log(Level.INFO,"> +o " + p[4]); } else { LOGGER.log(Level.INFO,"> -o " + p[4]); } } else if (line.toLowerCase().contains("disconnected")) { LOGGER.log(Level.INFO, line); this.connect(); } else { LOGGER.log(Level.INFO,"> " + line); } } } catch (IOException e) { e.printStackTrace(); } } connect () method of TwitchBot class
public void connect(String ip, int port) { if (isRunning()) return; try{ if (user == null || user == "") { LOGGER.log(Level.SEVERE, "Please select a valid Username"); System.exit(1); return; } if (oauth_key == null || oauth_key == "") { LOGGER.log(Level.SEVERE,"Please select a valid Oauth_Key"); System.exit(2); return; } @SuppressWarnings("resource") Socket socket = new Socket(ip, port); this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8)); this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream(),StandardCharsets.UTF_8)); this.writer.write("PASS " + oauth_key + "\r\n"); this.writer.write("NICK " + user + "\r\n"); this.writer.write("USER " + this.getVersion() + " \r\n"); this.writer.write("CAP REQ :twitch.tv/commands \r\n"); this.writer.write("CAP REQ :twitch.tv/membership \r\n"); this.writer.flush(); String line = ""; while ((line = this.reader.readLine()) != null) { if (line.indexOf("004") >= 0) { LOGGER.log(Level.INFO,"Connected >> " + user + " ~ irc.twitch.tv"); break; }else { LOGGER.log(Level.INFO,line); } } } catch (IOException e) { e.printStackTrace(); } } Thanks in advance for your help.