Skip to content

Commit

Permalink
fixed dropping connections
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercake10 committed Feb 8, 2018
1 parent 6199a11 commit 2cbd924
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>TelegramChat</groupId>
<artifactId>TelegramChat</artifactId>
<version>1.0.9.9</version>
<version>1.0.10</version>
<name>TelegramChat</name>
<url>https://www.spigotmc.org/resources/telegramchat.16576/</url>
<repositories>
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/de/Linus122/TelegramChat/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ public void onEnable(){
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);
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/de/Linus122/TelegramChat/Telegram.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public boolean reconnect(){
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){
Expand All @@ -50,16 +51,15 @@ public boolean reconnect(){
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")) {
Expand All @@ -79,10 +79,10 @@ public void getUpdate(){
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;
Expand Down Expand Up @@ -119,6 +119,7 @@ public void getUpdate(){
}
}
}
return true;
}

public void sendMsg(int id, String msg){
Expand Down

0 comments on commit 2cbd924

Please sign in to comment.