ソースを参照

fixed dropping connections

mastercake10 6 年 前
コミット
2cbd9242b3

+ 1 - 1
pom.xml

@@ -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>

+ 7 - 2
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);

+ 7 - 6
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){