Skip to content

Commit

Permalink
Merge pull request #94 from jwdeveloper/develop-1.8.5
Browse files Browse the repository at this point in the history
Develop 1.8.5
  • Loading branch information
kohlerpop1 authored Jul 31, 2024
2 parents cb20c3d + fb458e7 commit c160259
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public class LiveClientSettings {

/**
* Interval of time in milliseconds between pings to TikTok
* @apiNote Min: 250 (0.25 seconds), Default: 5000 (5 seconds)
* @apiNote Min: 250 (0.25 seconds), Default: 10000 (10 seconds - TikTok Default)
*/
private long pingInterval = 5000;
private long pingInterval = 10000;

/** Throw an exception on 18+ Age Restriction */
private boolean throwOnAgeRestriction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public LiveClient build() {

//networking
dependance.registerSingleton(HttpClientFactory.class);
dependance.registerSingleton(TikTokWebSocketPingingTask.class);
dependance.registerSingleton(WebSocketHeartbeatTask.class);
if (clientSettings.isOffline()) {
dependance.registerSingleton(LiveSocketClient.class, TikTokWebSocketOfflineClient.class);
dependance.registerSingleton(LiveHttpClient.class, TikTokLiveHttpOfflineClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ public class TikTokWebSocketClient implements LiveSocketClient {
private final LiveClientSettings clientSettings;
private final LiveMessagesHandler messageHandler;
private final LiveEventsHandler tikTokEventHandler;
private final TikTokWebSocketPingingTask pingingTask;
private final WebSocketHeartbeatTask heartbeatTask;
private WebSocketClient webSocketClient;
private boolean isConnected;

public TikTokWebSocketClient(
LiveClientSettings clientSettings,
LiveMessagesHandler messageHandler,
LiveEventsHandler tikTokEventHandler,
TikTokWebSocketPingingTask pingingTask)
WebSocketHeartbeatTask heartbeatTask)
{
this.clientSettings = clientSettings;
this.messageHandler = messageHandler;
this.tikTokEventHandler = tikTokEventHandler;
this.pingingTask = pingingTask;
this.heartbeatTask = heartbeatTask;
isConnected = false;
}

Expand Down Expand Up @@ -84,7 +84,7 @@ public void start(LiveConnectionData.Response connectionData, LiveClient liveCli
private void connectDefault() {
try {
webSocketClient.connect();
pingingTask.run(webSocketClient, clientSettings.getPingInterval());
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
isConnected = true;
} catch (Exception e) {
isConnected = false;
Expand Down Expand Up @@ -120,7 +120,7 @@ public X509Certificate[] getAcceptedIssuers() {
proxySettings.remove();
continue;
}
pingingTask.run(webSocketClient, clientSettings.getPingInterval());
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
isConnected = true;
break;
}
Expand All @@ -141,7 +141,7 @@ public boolean tryProxyConnection(ProxyClientSettings proxySettings, ProxyData p
public void stop() {
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
webSocketClient.closeConnection(0, "");
pingingTask.stop();
heartbeatTask.stop();
}
webSocketClient = null;
isConnected = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@
*/
package io.github.jwdeveloper.tiktok.websocket;

import io.github.jwdeveloper.tiktok.live.LiveEventsHandler;
import org.java_websocket.WebSocket;

public class TikTokWebSocketPingingTask {
public class WebSocketHeartbeatTask
{
private Thread thread;
private boolean isRunning = false;
private final int MAX_TIMEOUT = 250;
private final int SLEEP_TIME = 500;
private final byte[] heartbeatBytes = {58, 2, 104, 98}; // Byte Array of "3A026862" which is TikTok's custom heartbeat value

public void run(WebSocket webSocket, long pingTaskTime) {
stop();
thread = new Thread(() -> pingTask(webSocket, pingTaskTime), "pinging-task");
thread = new Thread(() -> heartbeatTask(webSocket, pingTaskTime), "heartbeat-task");
isRunning = true;
thread.start();
}
Expand All @@ -44,11 +45,11 @@ public void stop() {
isRunning = false;
}

private void pingTask(WebSocket webSocket, long pingTaskTime) {
private void heartbeatTask(WebSocket webSocket, long pingTaskTime) {
while (isRunning) {
try {
if (webSocket.isOpen()) {
webSocket.sendPing();
webSocket.send(heartbeatBytes);
Thread.sleep(pingTaskTime + (int) (Math.random() * MAX_TIMEOUT));
} else
Thread.sleep(SLEEP_TIME);
Expand Down

0 comments on commit c160259

Please sign in to comment.