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;
|
private HttpClientSettings httpSettings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optional: Sometimes not every messages from chat are send to TikTokLiveJava to fix this issue you can set sessionId
|
* Loop time in milliseconds between pings to TikTok
|
||||||
* documentation how to obtain sessionId https://github.com/isaackogan/TikTok-Live-Connector#send-chat-messages
|
* @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;
|
private String sessionId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ public class TikTokWebSocketClient implements SocketClient {
|
|||||||
private void connectDefault() {
|
private void connectDefault() {
|
||||||
try {
|
try {
|
||||||
webSocketClient.connect();
|
webSocketClient.connect();
|
||||||
pingingTask.run(webSocketClient);
|
pingingTask.run(webSocketClient, clientSettings.getPingTaskTime());
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
@@ -112,7 +112,7 @@ public class TikTokWebSocketClient implements SocketClient {
|
|||||||
proxySettings.remove();
|
proxySettings.remove();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
pingingTask.run(webSocketClient);
|
pingingTask.run(webSocketClient, clientSettings.getPingTaskTime());
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ public class TikTokWebSocketPingingTask
|
|||||||
{
|
{
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
private boolean isRunning = false;
|
private boolean isRunning = false;
|
||||||
private final int MIN_TIMEOUT = 250;
|
private final int MAX_TIMEOUT = 250;
|
||||||
private final int MAX_TIMEOUT = 500;
|
private final int SLEEP_TIME = 500;
|
||||||
|
|
||||||
public void run(WebSocket webSocket)
|
public void run(WebSocket webSocket, long pingTaskTime)
|
||||||
{
|
{
|
||||||
stop();
|
stop();
|
||||||
thread = new Thread(() -> pingTask(webSocket));
|
thread = new Thread(() -> pingTask(webSocket, pingTaskTime));
|
||||||
isRunning = true;
|
isRunning = true;
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
@@ -26,20 +26,18 @@ public class TikTokWebSocketPingingTask
|
|||||||
isRunning = false;
|
isRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void pingTask(WebSocket webSocket, long pingTaskTime)
|
||||||
private void pingTask(WebSocket webSocket)
|
|
||||||
{
|
{
|
||||||
var random = new Random();
|
var random = new Random();
|
||||||
while (isRunning) {
|
while (isRunning) {
|
||||||
try {
|
try {
|
||||||
if (!webSocket.isOpen()) {
|
if (!webSocket.isOpen()) {
|
||||||
Thread.sleep(100);
|
Thread.sleep(SLEEP_TIME);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
webSocket.sendPing();
|
webSocket.sendPing();
|
||||||
|
|
||||||
var timeout = random.nextInt(MAX_TIMEOUT)+MIN_TIMEOUT;
|
Thread.sleep(pingTaskTime+random.nextInt(MAX_TIMEOUT));
|
||||||
Thread.sleep(timeout);
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
isRunning = false;
|
isRunning = false;
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ package io.github.jwdeveloper.tiktok;
|
|||||||
|
|
||||||
|
|
||||||
import io.github.jwdeveloper.tiktok.extension.collector.TikTokLiveCollector;
|
import io.github.jwdeveloper.tiktok.extension.collector.TikTokLiveCollector;
|
||||||
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -40,7 +41,7 @@ public class CollectorExample {
|
|||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
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.setConnectionUrl("mongodb+srv://" + mongoUser + ":" + mongoPassword + "@" + mongoDatabase + "/?retryWrites=true&w=majority");
|
||||||
settings.setDatabaseName("tiktok");
|
settings.setDatabaseName("tiktok");
|
||||||
@@ -59,10 +60,10 @@ public class CollectorExample {
|
|||||||
{
|
{
|
||||||
event.getException().printStackTrace();
|
event.getException().printStackTrace();
|
||||||
})
|
})
|
||||||
.addListener(collector.newListener(additionalDataFields, document ->
|
.addListener(collector.newListener(additionalDataFields, o ->
|
||||||
{
|
{
|
||||||
//filtering document data before it is inserted to database
|
//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 false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public class MongoDataCollectorSettings {
|
|||||||
|
|
||||||
private String sessionTag;
|
private String sessionTag;
|
||||||
|
|
||||||
public void setConnectionUrl(Consumer<MongoDBConnectionStringBuilder> consumer) {
|
public void setConnectionUrlConsumer(Consumer<MongoDBConnectionStringBuilder> consumer) {
|
||||||
var builder = new MongoDBConnectionStringBuilder();
|
var builder = new MongoDBConnectionStringBuilder();
|
||||||
consumer.accept(builder);
|
consumer.accept(builder);
|
||||||
connectionUrl = builder.build();
|
connectionUrl = builder.build();
|
||||||
|
|||||||
Reference in New Issue
Block a user