【问题标题】:java.net.SocketException: Connection reset by peer. In custom twitch botjava.net.SocketException:对等方重置连接。在自定义抽搐机器人
【发布时间】:2019-05-03 21:03:50
【问题描述】:

我使用 cavariux 库创建了自定义 Twitch 机器人。我在主类中调用了这个方法。

bot.setOauth_Key("oauth:key_Value");
bot.connect();
bot.joinChannel(channel.toString());
bot.start();

机器人的 5-6 次启动中大约有一次伴随着异常

java.net.SocketException: 连接被对端重置

。堆栈跟踪表明异常从这一行开始。

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

method start() 的 TwitchBot 类中。除了在方法 connect(String ip, int port) 中添加 utf 编码外,我没有更改此库的代码。

this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8));
this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));

我已经在不同的 PC 上测试了我的机器人。在某些机器上我没有这个问题。在某些情况下,我更经常遇到此异常。 这是 TwitchBot 类中 start() 方法的代码。

 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();
            }
        }

这是 TwitchBot 类中 connect() 方法的代码。

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();
        }
    }

感谢您的帮助

【问题讨论】:

    标签: java exception bots twitch


    【解决方案1】:

    此错误意味着对等方(即 Twitch 服务器)突然关闭了您的连接。详情请见this answer

    我不知道您是否可以采取措施解决这个问题,因为它可能有各种外部来源(同行崩溃......)。或许您可以稍等片刻再尝试重新连接(请注意,如果您连接太频繁,您可能会被列入黑名单)。

    【讨论】:

    • 感谢您的回复。但事实是我只在某些机器上遇到了这个问题。在我的电脑和其他一些电脑上,我没有这个问题,或者在极少数情况下有这个问题
    • @Valeyard 你确定它是系统的吗?如果是这样,代理或防火墙后面的“问题”机器不是吗?
    • 我不确定。将尝试与 Twitch 支持联系,并将在此处写下他们将回答的内容。无论如何谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2021-03-10
    相关资源
    最近更新 更多