fixed dropping connections

This commit is contained in:
mastercake10 2018-02-08 19:52:15 +01:00
parent 6199a119e0
commit 2cbd9242b3
3 changed files with 15 additions and 9 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>TelegramChat</groupId> <groupId>TelegramChat</groupId>
<artifactId>TelegramChat</artifactId> <artifactId>TelegramChat</artifactId>
<version>1.0.9.9</version> <version>1.0.10</version>
<name>TelegramChat</name> <name>TelegramChat</name>
<url>https://www.spigotmc.org/resources/telegramchat.16576/</url> <url>https://www.spigotmc.org/resources/telegramchat.16576/</url>
<repositories> <repositories>

View File

@ -67,10 +67,15 @@ public class Main extends JavaPlugin implements Listener{
telegramHook = new Telegram(); telegramHook = new Telegram();
telegramHook.auth(data.token); telegramHook.auth(data.token);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){ Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
boolean connectionLost = false;
public void run(){ public void run(){
if(connectionLost){
boolean success = telegramHook.reconnect();
if(success) connectionLost = false;
}
if(telegramHook.connected){ if(telegramHook.connected){
telegramHook.getUpdate(); connectionLost = !telegramHook.getUpdate();
} }
} }
}, 20L, 20L); }, 20L, 20L);

View File

@ -42,6 +42,7 @@ public class Telegram {
try{ try{
JsonObject obj = sendGet("https://api.telegram.org/bot" + token + "/getMe"); JsonObject obj = sendGet("https://api.telegram.org/bot" + token + "/getMe");
authJson = obj; authJson = obj;
System.out.print("[Telegram] Established a connection with the telegram servers.");
connected = true; connected = true;
return true; return true;
}catch(Exception e){ }catch(Exception e){
@ -50,16 +51,15 @@ public class Telegram {
return false; return false;
} }
} }
public void getUpdate(){ public boolean getUpdate(){
JsonObject up = null; JsonObject up = null;
try { try {
up = sendGet("https://api.telegram.org/bot" + Main.data.token + "/getUpdates?offset=" + (lastUpdate + 1)); up = sendGet("https://api.telegram.org/bot" + Main.data.token + "/getUpdates?offset=" + (lastUpdate + 1));
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block return false;
e.printStackTrace();
} }
if(up == null){ if(up == null){
reconnect(); return false;
} }
if(up.has("result")){ if(up.has("result")){
for (JsonElement ob : up.getAsJsonArray("result")) { for (JsonElement ob : up.getAsJsonArray("result")) {
@ -79,10 +79,10 @@ public class Telegram {
for(char c : text.toCharArray()){ for(char c : text.toCharArray()){
if((int) c == 55357){ if((int) c == 55357){
this.sendMsg(id, "Emoticons are not allowed, sorry!"); 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(text.equals("/start")){
if(Main.data.firstUse){ if(Main.data.firstUse){
Main.data.firstUse = false; Main.data.firstUse = false;
@ -119,6 +119,7 @@ public class Telegram {
} }
} }
} }
return true;
} }
public void sendMsg(int id, String msg){ public void sendMsg(int id, String msg){