mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
Fixed Live User Data Mapper throwing MalformedJsonException!
This commit is contained in:
@@ -86,9 +86,23 @@ public class LiveClientSettings {
|
||||
private HttpClientSettings httpSettings;
|
||||
|
||||
/**
|
||||
* Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
|
||||
* documentation how to obtain sessionId https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages
|
||||
* Loop time in milliseconds between pings to TikTok
|
||||
* @apiNote Min: 250 (0.25 seconds), Max: 30000 (30 seconds), Default: 5000 (5 seconds)
|
||||
*/
|
||||
private long pingTaskTime = 5000;
|
||||
|
||||
public void setPingTaskTime(long pingTaskTime) {
|
||||
if (pingTaskTime < 250)
|
||||
throw new IllegalArgumentException("Minimum allowed value 250 millseconds");
|
||||
if (pingTaskTime > 30000)
|
||||
throw new IllegalArgumentException("Maximum allowed value 30000 milliseconds");
|
||||
this.pingTaskTime = pingTaskTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
|
||||
* @see <a href="https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages">Documentation: How to obtain sessionId</a>
|
||||
*/
|
||||
private String sessionId;
|
||||
|
||||
/**
|
||||
|
||||
@@ -82,7 +82,7 @@ public class TikTokWebSocketClient implements SocketClient {
|
||||
private void connectDefault() {
|
||||
try {
|
||||
webSocketClient.connect();
|
||||
pingingTask.run(webSocketClient);
|
||||
pingingTask.run(webSocketClient, clientSettings.getPingTaskTime());
|
||||
isConnected = true;
|
||||
} catch (Exception e) {
|
||||
isConnected = false;
|
||||
@@ -112,7 +112,7 @@ public class TikTokWebSocketClient implements SocketClient {
|
||||
proxySettings.remove();
|
||||
continue;
|
||||
}
|
||||
pingingTask.run(webSocketClient);
|
||||
pingingTask.run(webSocketClient, clientSettings.getPingTaskTime());
|
||||
isConnected = true;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ public class TikTokWebSocketPingingTask
|
||||
{
|
||||
private Thread thread;
|
||||
private boolean isRunning = false;
|
||||
private final int MIN_TIMEOUT = 250;
|
||||
private final int MAX_TIMEOUT = 500;
|
||||
private final int MAX_TIMEOUT = 250;
|
||||
private final int SLEEP_TIME = 500;
|
||||
|
||||
public void run(WebSocket webSocket)
|
||||
public void run(WebSocket webSocket, long pingTaskTime)
|
||||
{
|
||||
stop();
|
||||
thread = new Thread(() -> pingTask(webSocket));
|
||||
thread = new Thread(() -> pingTask(webSocket, pingTaskTime));
|
||||
isRunning = true;
|
||||
thread.start();
|
||||
}
|
||||
@@ -26,20 +26,18 @@ public class TikTokWebSocketPingingTask
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
|
||||
private void pingTask(WebSocket webSocket)
|
||||
private void pingTask(WebSocket webSocket, long pingTaskTime)
|
||||
{
|
||||
var random = new Random();
|
||||
while (isRunning) {
|
||||
try {
|
||||
if (!webSocket.isOpen()) {
|
||||
Thread.sleep(100);
|
||||
Thread.sleep(SLEEP_TIME);
|
||||
continue;
|
||||
}
|
||||
webSocket.sendPing();
|
||||
|
||||
var timeout = random.nextInt(MAX_TIMEOUT)+MIN_TIMEOUT;
|
||||
Thread.sleep(timeout);
|
||||
Thread.sleep(pingTaskTime+random.nextInt(MAX_TIMEOUT));
|
||||
}
|
||||
catch (Exception e) {
|
||||
isRunning = false;
|
||||
|
||||
@@ -24,6 +24,7 @@ package io.github.jwdeveloper.tiktok;
|
||||
|
||||
|
||||
import io.github.jwdeveloper.tiktok.extension.collector.TikTokLiveCollector;
|
||||
import org.bson.Document;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
@@ -40,7 +41,7 @@ public class CollectorExample {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
var collector = TikTokLiveCollector.use(settings ->
|
||||
var collector = TikTokLiveCollector.useMongo(settings ->
|
||||
{
|
||||
settings.setConnectionUrl("mongodb+srv://" + mongoUser + ":" + mongoPassword + "@" + mongoDatabase + "/?retryWrites=true&w=majority");
|
||||
settings.setDatabaseName("tiktok");
|
||||
@@ -59,10 +60,10 @@ public class CollectorExample {
|
||||
{
|
||||
event.getException().printStackTrace();
|
||||
})
|
||||
.addListener(collector.newListener(additionalDataFields, document ->
|
||||
.addListener(collector.newListener(additionalDataFields, o ->
|
||||
{
|
||||
//filtering document data before it is inserted to database
|
||||
if (document.get("dataType") == "message") {
|
||||
if (o instanceof Document document && document.get("dataType") == "message") {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -73,4 +74,4 @@ public class CollectorExample {
|
||||
System.in.read();
|
||||
collector.disconnectDatabase();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ public class MongoDataCollectorSettings {
|
||||
|
||||
private String sessionTag;
|
||||
|
||||
public void setConnectionUrl(Consumer<MongoDBConnectionStringBuilder> consumer) {
|
||||
public void setConnectionUrlConsumer(Consumer<MongoDBConnectionStringBuilder> consumer) {
|
||||
var builder = new MongoDBConnectionStringBuilder();
|
||||
consumer.accept(builder);
|
||||
connectionUrl = builder.build();
|
||||
|
||||
Reference in New Issue
Block a user