mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
Compare commits
10 Commits
1.11.1-Rel
...
1.11.6-Rel
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6486519876 | ||
|
|
96cf28e5d5 | ||
|
|
cfdced9645 | ||
|
|
7589a2ac4a | ||
|
|
a0c445656c | ||
|
|
1e78fdda89 | ||
|
|
57f33b2efa | ||
|
|
85cba9fff2 | ||
|
|
b7977469a0 | ||
|
|
8910c6a491 |
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.11.0-Release</version>
|
||||
<version>1.11.5-Release</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>API</artifactId>
|
||||
|
||||
@@ -66,5 +66,5 @@ public interface LiveHttpClient
|
||||
|
||||
LiveConnectionData.Response fetchLiveConnectionData(LiveConnectionData.Request request);
|
||||
|
||||
boolean sendChat(LiveRoomInfo roomInfo, String content);
|
||||
boolean sendChat(LiveRoomInfo roomInfo, String content, String sessionId, String ttTargetIdc);
|
||||
}
|
||||
@@ -102,4 +102,12 @@ public interface LiveClient {
|
||||
* <p>We cannot fix this as it is a TikTok issue, not a library issue.
|
||||
*/
|
||||
boolean sendChat(String content);
|
||||
|
||||
/**
|
||||
* Send a chat message to the connected room
|
||||
* @return true if successful, otherwise false
|
||||
* @apiNote This is known to return true on some sessionIds despite failing!
|
||||
* <p>We cannot fix this as it is a TikTok issue, not a library issue.
|
||||
*/
|
||||
boolean sendChat(String content, String sessionId, String ttTargetIdc);
|
||||
}
|
||||
@@ -62,12 +62,11 @@ message Text {
|
||||
int32 type = 1;
|
||||
TextFormat format = 2;
|
||||
string stringValue = 11;
|
||||
oneof textPieceType
|
||||
{
|
||||
TextPieceUser userValue = 21;
|
||||
TextPieceGift giftValue = 22;
|
||||
}
|
||||
TextPieceHeart heartValue = 23;
|
||||
TextPiecePatternRef patternRefValue = 24;
|
||||
TextPieceImage imageValue = 25;
|
||||
}
|
||||
|
||||
message TextFormat {
|
||||
@@ -83,7 +82,7 @@ message Text {
|
||||
|
||||
message TextPieceGift {
|
||||
int32 giftId = 1;
|
||||
PatternRef nameRef = 2;
|
||||
TextPiecePatternRef nameRef = 2;
|
||||
ShowType showType = 3; // Enum
|
||||
int64 colorId = 4;
|
||||
}
|
||||
@@ -98,16 +97,19 @@ message Text {
|
||||
bool withColon = 2;
|
||||
}
|
||||
|
||||
message PatternRef {
|
||||
string key = 1;
|
||||
string default_pattern = 2;
|
||||
}
|
||||
|
||||
enum ShowType {
|
||||
SHOW_TYPE_NORMAL = 0;
|
||||
SHOW_TYPE_FADE_IN_OUT = 1;
|
||||
}
|
||||
|
||||
message TextPieceHeart {
|
||||
string color = 1;
|
||||
}
|
||||
|
||||
message TextPieceImage {
|
||||
Image image_model = 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// @Image
|
||||
@@ -151,7 +153,7 @@ message BadgeStruct {
|
||||
bool is_customized = 24;
|
||||
|
||||
message CombineBadge {
|
||||
int32 badge_display_type = 1;
|
||||
BadgeDisplayType badge_display_type = 1;
|
||||
Image icon = 2;
|
||||
TextBadge text = 3;
|
||||
string str = 4;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.11.0-Release</version>
|
||||
<version>1.11.5-Release</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
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.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.data.events.control.*;
|
||||
@@ -39,7 +40,7 @@ import io.github.jwdeveloper.tiktok.websocket.*;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -89,12 +90,11 @@ public class TikTokLiveClient implements LiveClient
|
||||
tikTokEventHandler.publish(this, new TikTokDisconnectedEvent("Exception: " + e.getMessage()));
|
||||
|
||||
if (e instanceof TikTokLiveOfflineHostException && clientSettings.isRetryOnConnectionFailure()) {
|
||||
try {
|
||||
Thread.sleep(clientSettings.getRetryConnectionTimeout().toMillis());
|
||||
} catch (Exception ignored) {}
|
||||
AsyncHandler.getReconnectScheduler().schedule(() -> {
|
||||
logger.info("Reconnecting");
|
||||
tikTokEventHandler.publish(this, new TikTokReconnectingEvent());
|
||||
this.connect();
|
||||
}, clientSettings.getRetryConnectionTimeout().toMillis(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
@@ -199,7 +199,12 @@ public class TikTokLiveClient implements LiveClient
|
||||
|
||||
@Override
|
||||
public boolean sendChat(String content) {
|
||||
return httpClient.sendChat(roomInfo, content);
|
||||
return sendChat(content, clientSettings.getSessionId(), clientSettings.getTtTargetIdc());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendChat(String content, String sessionId, String ttTargetIdc) {
|
||||
return httpClient.sendChat(roomInfo, content, sessionId, ttTargetIdc);
|
||||
}
|
||||
|
||||
public void connectAsync(Consumer<LiveClient> onConnection) {
|
||||
|
||||
@@ -133,7 +133,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
||||
|
||||
//networking
|
||||
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()) {
|
||||
dependance.registerSingleton(LiveSocketClient.class, TikTokWebSocketOfflineClient.class);
|
||||
dependance.registerSingleton(LiveHttpClient.class, TikTokLiveHttpOfflineClient.class);
|
||||
|
||||
@@ -182,23 +182,23 @@ public class TikTokLiveHttpClient implements LiveHttpClient
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendChat(LiveRoomInfo roomInfo, String content) {
|
||||
public boolean sendChat(LiveRoomInfo roomInfo, String content, String sessionId, String ttTargetIdc) {
|
||||
var proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
|
||||
if (proxyClientSettings.isEnabled()) {
|
||||
while (proxyClientSettings.hasNext()) {
|
||||
try {
|
||||
return requestSendChat(roomInfo, content);
|
||||
return requestSendChat(roomInfo, content, sessionId, ttTargetIdc);
|
||||
} catch (TikTokProxyRequestException ignored) {}
|
||||
}
|
||||
}
|
||||
return requestSendChat(roomInfo, content);
|
||||
return requestSendChat(roomInfo, content, sessionId, ttTargetIdc);
|
||||
}
|
||||
|
||||
public boolean requestSendChat(LiveRoomInfo roomInfo, String content) {
|
||||
public boolean requestSendChat(LiveRoomInfo roomInfo, String content, String sessionId, String ttTargetIdc) {
|
||||
JsonObject body = new JsonObject();
|
||||
body.addProperty("content", content);
|
||||
body.addProperty("sessionId", clientSettings.getSessionId());
|
||||
body.addProperty("ttTargetIdc", clientSettings.getTtTargetIdc());
|
||||
body.addProperty("sessionId", sessionId);
|
||||
body.addProperty("ttTargetIdc", ttTargetIdc);
|
||||
body.addProperty("roomId", roomInfo.getRoomId());
|
||||
HttpClientBuilder builder = httpFactory.client(clientSettings.isUseEulerstreamEnterprise() ? TIKTOK_CHAT_ENTERPRISE_URL : TIKTOK_CHAT_URL)
|
||||
.withHeader("Content-Type", "application/json");
|
||||
|
||||
@@ -64,7 +64,7 @@ public class TikTokLiveHttpOfflineClient implements LiveHttpClient {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendChat(LiveRoomInfo roomInfo, String content) {
|
||||
public boolean sendChat(LiveRoomInfo roomInfo, String content, String sessionId, String ttTargetIdc) {
|
||||
// DO NOTHING
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -70,6 +70,6 @@ public class TikTokRoomInfo implements LiveRoomInfo
|
||||
this.hostName = roomInfo.getHostName();
|
||||
this.title = roomInfo.getTitle();
|
||||
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;
|
||||
});
|
||||
}
|
||||
@@ -66,7 +66,6 @@ public class LiveUserDataMapper
|
||||
|
||||
roomInfo.setTitle(liveRoom.get("title").getAsString());
|
||||
roomInfo.setStartTime(liveRoom.get("startTime").getAsLong());
|
||||
roomInfo.setTitle(liveRoom.get("title").getAsString());
|
||||
roomInfo.setViewersCount(liveRoom.getAsJsonObject("liveRoomStats").get("userCount").getAsInt());
|
||||
roomInfo.setTotalViewersCount(liveRoom.getAsJsonObject("liveRoomStats").get("enterCount").getAsInt());
|
||||
roomInfo.setAgeRestricted(jsonObject.get("statusCode").getAsInt() == TikTokLiveHttpClient.TIKTOK_AGE_RESTRICTED_CODE);
|
||||
@@ -92,9 +91,51 @@ public class LiveUserDataMapper
|
||||
roomInfo.setHostName(foundUser.getName());
|
||||
|
||||
return new LiveUserData.Response(json, statusEnum, roomInfo);
|
||||
} catch (JsonSyntaxException | IllegalStateException e) {
|
||||
} catch (JsonSyntaxException | IllegalStateException | NullPointerException e) {
|
||||
logger.warning("Malformed Json: '"+json+"' - Error Message: "+e.getMessage());
|
||||
return new LiveUserData.Response(json, LiveUserData.UserStatus.NotFound, null);
|
||||
}
|
||||
}
|
||||
|
||||
public static LiveUserData.Response mapEulerstream(JsonObject jsonObject, Logger logger) {
|
||||
try {
|
||||
JsonObject roomInfoJson = jsonObject.getAsJsonObject("roomInfo");
|
||||
JsonObject userJson = jsonObject.getAsJsonObject("user");
|
||||
|
||||
var roomId = roomInfoJson.get("id").getAsString();
|
||||
var status = roomInfoJson.get("status").getAsInt();
|
||||
|
||||
TikTokRoomInfo roomInfo = new TikTokRoomInfo();
|
||||
roomInfo.setRoomId(roomId);
|
||||
roomInfo.setTitle(roomInfoJson.get("title").getAsString());
|
||||
roomInfo.setStartTime(roomInfoJson.get("startTime").getAsLong());
|
||||
roomInfo.setViewersCount(roomInfoJson.get("currentViewers").getAsInt());
|
||||
roomInfo.setTotalViewersCount(roomInfoJson.get("totalViewers").getAsInt());
|
||||
|
||||
var statusEnum = switch (status) {
|
||||
case 2 -> LiveUserData.UserStatus.Live;
|
||||
case 3 -> LiveUserData.UserStatus.LivePaused;
|
||||
case 4 -> LiveUserData.UserStatus.Offline;
|
||||
default -> LiveUserData.UserStatus.NotFound;
|
||||
};
|
||||
|
||||
User foundUser = new User(
|
||||
Long.parseLong(userJson.get("numericUid").getAsString()),
|
||||
userJson.get("uniqueId").getAsString(),
|
||||
userJson.get("nickname").getAsString(),
|
||||
userJson.get("signature").getAsString(),
|
||||
new Picture(userJson.get("avatarUrl").getAsString()),
|
||||
userJson.get("following").getAsLong(),
|
||||
userJson.get("followers").getAsLong(),
|
||||
List.of());
|
||||
|
||||
roomInfo.setHost(foundUser);
|
||||
roomInfo.setHostName(foundUser.getName());
|
||||
|
||||
return new LiveUserData.Response(jsonObject.toString(), statusEnum, roomInfo);
|
||||
} catch (JsonSyntaxException | IllegalStateException | NullPointerException e) {
|
||||
logger.warning("Malformed Json: '"+jsonObject.toString()+"' - Error Message: "+e.getMessage());
|
||||
return new LiveUserData.Response(jsonObject.toString(), LiveUserData.UserStatus.NotFound, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,7 +142,7 @@ public class TikTokWebSocketClient implements LiveSocketClient {
|
||||
case DISCONNECT -> webSocketClient.closeConnection(CloseFrame.NORMAL, "");
|
||||
default -> webSocketClient.close();
|
||||
}
|
||||
heartbeatTask.stop(webSocketClient);
|
||||
heartbeatTask.stop();
|
||||
}
|
||||
webSocketClient = null;
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
*/
|
||||
package io.github.jwdeveloper.tiktok.websocket;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.common.AsyncHandler;
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
import java.util.*;
|
||||
@@ -29,47 +30,30 @@ import java.util.concurrent.*;
|
||||
|
||||
public class WebSocketHeartbeatTask
|
||||
{
|
||||
// Single shared pool for all heartbeat tasks
|
||||
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, r -> {
|
||||
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 ScheduledFuture<?> task;
|
||||
private Long commTime;
|
||||
|
||||
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) {
|
||||
stop(webSocket); // remove existing task if any
|
||||
stop(); // remove existing task if any
|
||||
|
||||
tasks.put(webSocket, scheduler.scheduleAtFixedRate(() -> {
|
||||
task = AsyncHandler.getHeartBeatScheduler().scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
if (webSocket.isOpen()) {
|
||||
webSocket.send(heartbeatBytes);
|
||||
commTime.put(webSocket, System.currentTimeMillis());
|
||||
} else {
|
||||
Long time = commTime.get(webSocket);
|
||||
if (time != null && System.currentTimeMillis() - time >= 60_000) // Stop if disconnected longer than 60s
|
||||
stop(webSocket);
|
||||
}
|
||||
commTime = System.currentTimeMillis();
|
||||
} else if (commTime != null && System.currentTimeMillis() - commTime >= 60_000) // Stop if disconnected longer than 60s
|
||||
stop();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
stop(webSocket);
|
||||
stop();
|
||||
}
|
||||
}, 0, pingTaskTime, TimeUnit.MILLISECONDS));
|
||||
}, 0, pingTaskTime, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public void stop(WebSocket webSocket) {
|
||||
ScheduledFuture<?> future = tasks.remove(webSocket);
|
||||
if (future != null)
|
||||
future.cancel(true);
|
||||
commTime.remove(webSocket);
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
tasks.values().forEach(f -> f.cancel(true));
|
||||
commTime.clear();
|
||||
scheduler.shutdownNow();
|
||||
public void stop() {
|
||||
if (task != null)
|
||||
task.cancel(true);
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ public class TikTokWebSocketEulerListener extends TikTokWebSocketListener
|
||||
switch (oMsg.get("type").getAsString()) { // Should only receive these 2 types ever
|
||||
case "workerInfo" -> liveClient.getLogger().info(oMsg.toString()); // Always 1st message
|
||||
case "roomInfo" -> { // Always 2nd message
|
||||
LiveUserData.Response data = LiveUserDataMapper.map(oMsg.getAsJsonObject("data").getAsJsonObject("data").getAsJsonObject("raw").toString(), liveClient.getLogger());
|
||||
LiveUserData.Response data = LiveUserDataMapper.mapEulerstream(oMsg.getAsJsonObject("data"), liveClient.getLogger());
|
||||
liveClient.getRoomInfo().copy(data.getRoomInfo());
|
||||
eventHandler.publish(liveClient, new TikTokRoomInfoEvent(liveClient.getRoomInfo()));
|
||||
}
|
||||
@@ -67,6 +67,7 @@ public class TikTokWebSocketEulerListener extends TikTokWebSocketListener
|
||||
} else
|
||||
throw new IllegalArgumentException("Invalid JsonObject: "+element);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
eventHandler.publish(liveClient, new TikTokErrorEvent(e));
|
||||
}
|
||||
if (isOpen()) {
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.11.0-Release</version>
|
||||
<version>1.11.5-Release</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<version>1.11.0-Release</version>
|
||||
<version>1.11.5-Release</version>
|
||||
</parent>
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.11.0-Release</version>
|
||||
<version>1.11.5-Release</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>extension-recorder</artifactId>
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -7,7 +7,7 @@
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>1.11.0-Release</version>
|
||||
<version>1.11.5-Release</version>
|
||||
<modules>
|
||||
<module>API</module>
|
||||
<module>Client</module>
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.11.0-Release</version>
|
||||
<version>1.11.5-Release</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user