From 2cbd9242b3d535f0aa28f54c7b3772cc82f823ac Mon Sep 17 00:00:00 2001 From: mastercake10 Date: Thu, 8 Feb 2018 19:52:15 +0100 Subject: [PATCH] fixed dropping connections --- pom.xml | 2 +- src/main/java/de/Linus122/TelegramChat/Main.java | 9 +++++++-- .../java/de/Linus122/TelegramChat/Telegram.java | 13 +++++++------ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 69fd427..55d52a1 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 TelegramChat TelegramChat - 1.0.9.9 + 1.0.10 TelegramChat https://www.spigotmc.org/resources/telegramchat.16576/ diff --git a/src/main/java/de/Linus122/TelegramChat/Main.java b/src/main/java/de/Linus122/TelegramChat/Main.java index cb304b0..8a2a474 100755 --- a/src/main/java/de/Linus122/TelegramChat/Main.java +++ b/src/main/java/de/Linus122/TelegramChat/Main.java @@ -67,10 +67,15 @@ public class Main extends JavaPlugin implements Listener{ telegramHook = new Telegram(); telegramHook.auth(data.token); - Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){ + Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){ + boolean connectionLost = false; public void run(){ + if(connectionLost){ + boolean success = telegramHook.reconnect(); + if(success) connectionLost = false; + } if(telegramHook.connected){ - telegramHook.getUpdate(); + connectionLost = !telegramHook.getUpdate(); } } }, 20L, 20L); diff --git a/src/main/java/de/Linus122/TelegramChat/Telegram.java b/src/main/java/de/Linus122/TelegramChat/Telegram.java index e86541e..e7d9e0d 100755 --- a/src/main/java/de/Linus122/TelegramChat/Telegram.java +++ b/src/main/java/de/Linus122/TelegramChat/Telegram.java @@ -42,6 +42,7 @@ public class Telegram { try{ JsonObject obj = sendGet("https://api.telegram.org/bot" + token + "/getMe"); authJson = obj; + System.out.print("[Telegram] Established a connection with the telegram servers."); connected = true; return true; }catch(Exception e){ @@ -50,16 +51,15 @@ public class Telegram { return false; } } - public void getUpdate(){ + public boolean getUpdate(){ JsonObject up = null; try { up = sendGet("https://api.telegram.org/bot" + Main.data.token + "/getUpdates?offset=" + (lastUpdate + 1)); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + return false; } if(up == null){ - reconnect(); + return false; } if(up.has("result")){ for (JsonElement ob : up.getAsJsonArray("result")) { @@ -79,10 +79,10 @@ public class Telegram { for(char c : text.toCharArray()){ if((int) c == 55357){ this.sendMsg(id, "Emoticons are not allowed, sorry!"); - return; + return true; } } - if(text.length() == 0) return; + if(text.length() == 0) return true; if(text.equals("/start")){ if(Main.data.firstUse){ Main.data.firstUse = false; @@ -119,6 +119,7 @@ public class Telegram { } } } + return true; } public void sendMsg(int id, String msg){