0

I created custom Twitch bot with using of cavariux library. I called this methods in main class.

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 exception

java.net.SocketException: Connection reset by peer

. The stack trace indicates that the exception starts on this line.

while ((line = this.reader.readLine( )) != null && !stopped)

in TwitchBot class in method start(). I didn't change code of this library except adding utf encoding in method connect(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));

I've tested my bot on different PCs. On some machines I don't have this issue. On some I got this exception more often. This is code of method start() in TwitchBot 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();
            }
        }

This is code of method connect() in 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();
        }
    }

Thank you for the help

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
Valeyard
  • 1
  • 1

1 Answers1

0

This error means that the peer (i.e. the Twitch server) closes abruptly your connection. See this answer for more details.

I don't know if you can do something to fix that because it can have various external origins (peer crash...). Maybe you can wait and try to reconnect later (note that you might be blacklisted if you connect too often).

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
  • Thank you for your reply. Bur the fact is that I have this problem only on some machines. On my pc and some others I don't have this issue or have it on rare occasions – Valeyard Dec 02 '18 at 10:31
  • @Valeyard are you sure it is that systematic? If so aren't the "problematic" machines behind a proxy or a firewall? – C.Champagne Dec 02 '18 at 11:22
  • I'm not sure. Will try to connect with Twitch support and will write here what they will answer. Thx anyway – Valeyard Dec 02 '18 at 11:35