mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 16:59:39 -05:00
Compare commits
4 Commits
1.11.1-Rel
...
1.11.3-Rel
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
57f33b2efa | ||
|
|
85cba9fff2 | ||
|
|
b7977469a0 | ||
|
|
8910c6a491 |
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<version>1.11.0-Release</version>
|
<version>1.11.2-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>API</artifactId>
|
<artifactId>API</artifactId>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<version>1.11.0-Release</version>
|
<version>1.11.2-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
package io.github.jwdeveloper.tiktok;
|
package io.github.jwdeveloper.tiktok;
|
||||||
|
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
|
import io.github.jwdeveloper.tiktok.common.AsyncHandler;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.*;
|
import io.github.jwdeveloper.tiktok.data.events.*;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||||
import io.github.jwdeveloper.tiktok.data.events.control.*;
|
import io.github.jwdeveloper.tiktok.data.events.control.*;
|
||||||
@@ -39,7 +40,7 @@ import io.github.jwdeveloper.tiktok.websocket.*;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.*;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -89,12 +90,11 @@ public class TikTokLiveClient implements LiveClient
|
|||||||
tikTokEventHandler.publish(this, new TikTokDisconnectedEvent("Exception: " + e.getMessage()));
|
tikTokEventHandler.publish(this, new TikTokDisconnectedEvent("Exception: " + e.getMessage()));
|
||||||
|
|
||||||
if (e instanceof TikTokLiveOfflineHostException && clientSettings.isRetryOnConnectionFailure()) {
|
if (e instanceof TikTokLiveOfflineHostException && clientSettings.isRetryOnConnectionFailure()) {
|
||||||
try {
|
AsyncHandler.getReconnectScheduler().schedule(() -> {
|
||||||
Thread.sleep(clientSettings.getRetryConnectionTimeout().toMillis());
|
logger.info("Reconnecting");
|
||||||
} catch (Exception ignored) {}
|
tikTokEventHandler.publish(this, new TikTokReconnectingEvent());
|
||||||
logger.info("Reconnecting");
|
this.connect();
|
||||||
tikTokEventHandler.publish(this, new TikTokReconnectingEvent());
|
}, clientSettings.getRetryConnectionTimeout().toMillis(), TimeUnit.MILLISECONDS);
|
||||||
this.connect();
|
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
|||||||
|
|
||||||
//networking
|
//networking
|
||||||
dependance.registerSingleton(HttpClientFactory.class);
|
dependance.registerSingleton(HttpClientFactory.class);
|
||||||
dependance.registerSingleton(WebSocketHeartbeatTask.class); // True global singleton - Static objects are located to serve as global
|
dependance.registerSingleton(WebSocketHeartbeatTask.class);
|
||||||
if (clientSettings.isOffline()) {
|
if (clientSettings.isOffline()) {
|
||||||
dependance.registerSingleton(LiveSocketClient.class, TikTokWebSocketOfflineClient.class);
|
dependance.registerSingleton(LiveSocketClient.class, TikTokWebSocketOfflineClient.class);
|
||||||
dependance.registerSingleton(LiveHttpClient.class, TikTokLiveHttpOfflineClient.class);
|
dependance.registerSingleton(LiveHttpClient.class, TikTokLiveHttpOfflineClient.class);
|
||||||
|
|||||||
@@ -70,6 +70,6 @@ public class TikTokRoomInfo implements LiveRoomInfo
|
|||||||
this.hostName = roomInfo.getHostName();
|
this.hostName = roomInfo.getHostName();
|
||||||
this.title = roomInfo.getTitle();
|
this.title = roomInfo.getTitle();
|
||||||
this.language = roomInfo.getLanguage();
|
this.language = roomInfo.getLanguage();
|
||||||
this.connectionState = roomInfo.getConnectionState();
|
// this.connectionState = roomInfo.getConnectionState(); // This should not be copied - Controlled elsewhere!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package io.github.jwdeveloper.tiktok.common;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
public class AsyncHandler
|
||||||
|
{
|
||||||
|
@Getter
|
||||||
|
private static final ScheduledExecutorService heartBeatScheduler = Executors.newScheduledThreadPool(1, r -> {
|
||||||
|
Thread t = new Thread(r, "heartbeat-pool");
|
||||||
|
t.setDaemon(true);
|
||||||
|
return t;
|
||||||
|
});
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private static final ScheduledExecutorService reconnectScheduler = Executors.newScheduledThreadPool(0, r -> {
|
||||||
|
Thread t = new Thread(r, "reconnect-pool");
|
||||||
|
t.setDaemon(true);
|
||||||
|
return t;
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -142,7 +142,7 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
|||||||
case DISCONNECT -> webSocketClient.closeConnection(CloseFrame.NORMAL, "");
|
case DISCONNECT -> webSocketClient.closeConnection(CloseFrame.NORMAL, "");
|
||||||
default -> webSocketClient.close();
|
default -> webSocketClient.close();
|
||||||
}
|
}
|
||||||
heartbeatTask.stop(webSocketClient);
|
heartbeatTask.stop();
|
||||||
}
|
}
|
||||||
webSocketClient = null;
|
webSocketClient = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.jwdeveloper.tiktok.websocket;
|
package io.github.jwdeveloper.tiktok.websocket;
|
||||||
|
|
||||||
|
import io.github.jwdeveloper.tiktok.common.AsyncHandler;
|
||||||
import org.java_websocket.WebSocket;
|
import org.java_websocket.WebSocket;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -29,47 +30,30 @@ import java.util.concurrent.*;
|
|||||||
|
|
||||||
public class WebSocketHeartbeatTask
|
public class WebSocketHeartbeatTask
|
||||||
{
|
{
|
||||||
// Single shared pool for all heartbeat tasks
|
private ScheduledFuture<?> task;
|
||||||
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, r -> {
|
private Long commTime;
|
||||||
Thread t = new Thread(r, "heartbeat-pool");
|
|
||||||
t.setDaemon(true);
|
|
||||||
return t;
|
|
||||||
});
|
|
||||||
private static final Map<WebSocket, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
|
|
||||||
private static final Map<WebSocket, Long> commTime = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
private final byte[] heartbeatBytes = Base64.getDecoder().decode("MgJwYjoCaGI="); // Used to be '3A026862' aka ':\x02hb', now is '2\x02pb:\x02hb'.
|
private final static byte[] heartbeatBytes = Base64.getDecoder().decode("MgJwYjoCaGI="); // Used to be '3A026862' aka ':\x02hb', now is '2\x02pb:\x02hb'.
|
||||||
|
|
||||||
public void run(WebSocket webSocket, long pingTaskTime) {
|
public void run(WebSocket webSocket, long pingTaskTime) {
|
||||||
stop(webSocket); // remove existing task if any
|
stop(); // remove existing task if any
|
||||||
|
|
||||||
tasks.put(webSocket, scheduler.scheduleAtFixedRate(() -> {
|
task = AsyncHandler.getHeartBeatScheduler().scheduleAtFixedRate(() -> {
|
||||||
try {
|
try {
|
||||||
if (webSocket.isOpen()) {
|
if (webSocket.isOpen()) {
|
||||||
webSocket.send(heartbeatBytes);
|
webSocket.send(heartbeatBytes);
|
||||||
commTime.put(webSocket, System.currentTimeMillis());
|
commTime = System.currentTimeMillis();
|
||||||
} else {
|
} else if (commTime != null && System.currentTimeMillis() - commTime >= 60_000) // Stop if disconnected longer than 60s
|
||||||
Long time = commTime.get(webSocket);
|
stop();
|
||||||
if (time != null && System.currentTimeMillis() - time >= 60_000) // Stop if disconnected longer than 60s
|
|
||||||
stop(webSocket);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
stop(webSocket);
|
stop();
|
||||||
}
|
}
|
||||||
}, 0, pingTaskTime, TimeUnit.MILLISECONDS));
|
}, 0, pingTaskTime, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop(WebSocket webSocket) {
|
public void stop() {
|
||||||
ScheduledFuture<?> future = tasks.remove(webSocket);
|
if (task != null)
|
||||||
if (future != null)
|
task.cancel(true);
|
||||||
future.cancel(true);
|
|
||||||
commTime.remove(webSocket);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void shutdown() {
|
|
||||||
tasks.values().forEach(f -> f.cancel(true));
|
|
||||||
commTime.clear();
|
|
||||||
scheduler.shutdownNow();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,7 +41,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<version>1.11.0-Release</version>
|
<version>1.11.2-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<version>1.11.0-Release</version>
|
<version>1.11.2-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<version>1.11.0-Release</version>
|
<version>1.11.2-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>extension-recorder</artifactId>
|
<artifactId>extension-recorder</artifactId>
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -7,7 +7,7 @@
|
|||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>1.11.0-Release</version>
|
<version>1.11.2-Release</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>API</module>
|
<module>API</module>
|
||||||
<module>Client</module>
|
<module>Client</module>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>TikTokLiveJava</artifactId>
|
<artifactId>TikTokLiveJava</artifactId>
|
||||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||||
<version>1.11.0-Release</version>
|
<version>1.11.2-Release</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user