mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
Including Pinging Task
This commit is contained in:
@@ -41,6 +41,8 @@ public class TikTokWebSocketClient implements SocketClient {
|
||||
private final TikTokLiveMessageHandler messageHandler;
|
||||
private final TikTokLiveEventHandler tikTokEventHandler;
|
||||
private WebSocketClient webSocketClient;
|
||||
|
||||
private TikTokWebSocketPingingTask pingingTask;
|
||||
private boolean isConnected;
|
||||
|
||||
public TikTokWebSocketClient(
|
||||
@@ -51,11 +53,11 @@ public class TikTokWebSocketClient implements SocketClient {
|
||||
this.messageHandler = messageHandler;
|
||||
this.tikTokEventHandler = tikTokEventHandler;
|
||||
isConnected = false;
|
||||
pingingTask = new TikTokWebSocketPingingTask();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient)
|
||||
{
|
||||
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient) {
|
||||
if (isConnected) {
|
||||
stop();
|
||||
}
|
||||
@@ -74,16 +76,16 @@ public class TikTokWebSocketClient implements SocketClient {
|
||||
// ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
|
||||
// if (proxyClientSettings.isEnabled())
|
||||
// connectProxy(proxyClientSettings);
|
||||
// else
|
||||
connectDefault();
|
||||
// else
|
||||
connectDefault();
|
||||
}
|
||||
|
||||
private void connectDefault() {
|
||||
try {
|
||||
webSocketClient.connect();
|
||||
pingingTask.run(webSocketClient);
|
||||
isConnected = true;
|
||||
} catch (Exception e)
|
||||
{
|
||||
} catch (Exception e) {
|
||||
isConnected = false;
|
||||
throw new TikTokLiveException("Failed to connect to the websocket", e);
|
||||
}
|
||||
@@ -110,16 +112,21 @@ public class TikTokWebSocketClient implements SocketClient {
|
||||
if (proxySettings.getType() == Proxy.Type.SOCKS) {
|
||||
SSLContext sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, new TrustManager[]{new X509TrustManager() {
|
||||
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {}
|
||||
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {}
|
||||
public X509Certificate[] getAcceptedIssuers() { return null; }
|
||||
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
|
||||
}
|
||||
|
||||
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
|
||||
}
|
||||
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
}}, null);
|
||||
webSocketClient.setSocketFactory(sc.getSocketFactory());
|
||||
}
|
||||
webSocketClient.connect();
|
||||
return true;
|
||||
} catch (Exception e)
|
||||
{
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -127,6 +134,7 @@ public class TikTokWebSocketClient implements SocketClient {
|
||||
public void stop() {
|
||||
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
|
||||
webSocketClient.closeConnection(0, "");
|
||||
pingingTask.stop();
|
||||
}
|
||||
webSocketClient = null;
|
||||
isConnected = false;
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package io.github.jwdeveloper.tiktok.websocket;
|
||||
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class TikTokWebSocketPingingTask
|
||||
{
|
||||
private Thread thread;
|
||||
private boolean isRunning = false;
|
||||
private final int MIN_TIMEOUT = 250;
|
||||
private final int MAX_TIMEOUT = 500;
|
||||
|
||||
|
||||
public void run(WebSocket webSocket)
|
||||
{
|
||||
stop();
|
||||
thread = new Thread(() ->
|
||||
{
|
||||
pingTask(webSocket);
|
||||
});
|
||||
isRunning =true;
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
if(thread != null)
|
||||
{
|
||||
thread.interrupt();
|
||||
}
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
|
||||
private void pingTask(WebSocket webSocket)
|
||||
{
|
||||
var random = new Random();
|
||||
while (isRunning)
|
||||
{
|
||||
try
|
||||
{
|
||||
if(!webSocket.isOpen())
|
||||
{
|
||||
Thread.sleep(100);
|
||||
continue;
|
||||
}
|
||||
webSocket.sendPing();
|
||||
|
||||
var timeout = random.nextInt(MAX_TIMEOUT)+MIN_TIMEOUT;
|
||||
Thread.sleep(timeout);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
isRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -30,16 +30,24 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class CollectorExample {
|
||||
|
||||
|
||||
private static String mongoUser;
|
||||
|
||||
private static String mongoPassword;
|
||||
|
||||
private static String mongoDatabase;
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
var collector = TikTokLiveCollector.use(settings ->
|
||||
{
|
||||
settings.setConnectionUrl("mongodb+srv://jwoln:qaz123456@jwdatabase.a15gw.mongodb.net/?retryWrites=true&w=majority");
|
||||
settings.setConnectionUrl("mongodb+srv://" + mongoUser + ":" + mongoPassword + "@" + mongoDatabase + "/?retryWrites=true&w=majority");
|
||||
settings.setDatabaseName("tiktok");
|
||||
});
|
||||
collector.connectDatabase();
|
||||
|
||||
var users = List.of("tehila_723", "dino123597", "domaxyzx", "dash4214","obserwacje_live");
|
||||
var users = List.of("tehila_723", "dino123597", "domaxyzx", "dash4214", "obserwacje_live");
|
||||
var sessionTag = "Dupa";
|
||||
for (var user : users) {
|
||||
TikTokLive.newClient(user)
|
||||
@@ -51,10 +59,9 @@ public class CollectorExample {
|
||||
{
|
||||
event.getException().printStackTrace();
|
||||
})
|
||||
.addListener(collector.newListener(Map.of("sessionTag", sessionTag),document ->
|
||||
.addListener(collector.newListener(Map.of("sessionTag", sessionTag), document ->
|
||||
{
|
||||
if(document.get("dataType") == "message")
|
||||
{
|
||||
if (document.get("dataType") == "message") {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -57,7 +57,7 @@ public class ListenerExample
|
||||
/**
|
||||
*
|
||||
* Method in TikTokEventListener should meet 4 requirements to be detected
|
||||
* - must have @TikTokEventHandler annotation
|
||||
* - must have @TikTokEventObserver annotation
|
||||
* - must have 2 parameters
|
||||
* - first parameter must be LiveClient
|
||||
* - second must be class that extending TikTokEvent
|
||||
|
||||
@@ -24,27 +24,34 @@ package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import java.net.Proxy;
|
||||
|
||||
public class ProxyExample
|
||||
{
|
||||
public class ProxyExample {
|
||||
public static void main(String[] args) throws Exception {
|
||||
TikTokLive.newClient(SimpleExample.TIKTOK_HOSTNAME)
|
||||
.configure(clientSettings -> {
|
||||
clientSettings.setPrintToConsole(true);
|
||||
clientSettings.getHttpSettings().configureProxy(proxySettings -> {
|
||||
proxySettings.setOnProxyUpdated(proxyData -> System.err.println("Next proxy: " + proxyData.toString()));
|
||||
proxySettings.setType(Proxy.Type.SOCKS);
|
||||
proxySettings.addProxy("localhost", 8080);
|
||||
});
|
||||
})
|
||||
.onConnected((liveClient, event) ->
|
||||
liveClient.getLogger().info("Connected "+liveClient.getRoomInfo().getHostName()))
|
||||
.onDisconnected((liveClient, event) ->
|
||||
liveClient.getLogger().info("Disconnect reason: "+event.getReason()))
|
||||
.onLiveEnded((liveClient, event) ->
|
||||
liveClient.getLogger().info("Live Ended"))
|
||||
.onError((liveClient, event) ->
|
||||
event.getException().printStackTrace())
|
||||
.buildAndConnect();
|
||||
.configure(clientSettings -> {
|
||||
clientSettings.setPrintToConsole(true);
|
||||
clientSettings.getHttpSettings().configureProxy(proxySettings -> {
|
||||
proxySettings.setOnProxyUpdated(proxyData -> System.err.println("Next proxy: " + proxyData.toString()));
|
||||
proxySettings.setType(Proxy.Type.SOCKS);
|
||||
proxySettings.addProxy("localhost", 8080);
|
||||
});
|
||||
})
|
||||
.onConnected((liveClient, event) ->
|
||||
{
|
||||
liveClient.getLogger().info("Connected " + liveClient.getRoomInfo().getHostName());
|
||||
})
|
||||
.onDisconnected((liveClient, event) ->
|
||||
{
|
||||
liveClient.getLogger().info("Disconnect reason: " + event.getReason());
|
||||
})
|
||||
.onLiveEnded((liveClient, event) ->
|
||||
{
|
||||
liveClient.getLogger().info("Live Ended");
|
||||
})
|
||||
.onError((liveClient, event) ->
|
||||
{
|
||||
event.getException().printStackTrace();
|
||||
})
|
||||
.buildAndConnect();
|
||||
System.in.read();
|
||||
}
|
||||
}
|
||||
@@ -652,24 +652,24 @@ public static void main(String[] args) throws IOException {
|
||||
|
||||
public static class CustomListener implements TikTokEventListener {
|
||||
|
||||
@TikTokEventHandler
|
||||
@TikTokEventObserver
|
||||
public void onLike(LiveClient liveClient, TikTokLikeEvent event) {
|
||||
System.out.println(event.toString());
|
||||
}
|
||||
|
||||
@TikTokEventHandler
|
||||
@TikTokEventObserver
|
||||
public void onError(LiveClient liveClient, TikTokErrorEvent event) {
|
||||
// event.getException().printStackTrace();
|
||||
}
|
||||
|
||||
@TikTokEventHandler
|
||||
@TikTokEventObserver
|
||||
public void onComment(LiveClient liveClient, TikTokCommentEvent event) {
|
||||
var userName = event.getUser().getName();
|
||||
var text = event.getText();
|
||||
liveClient.getLogger().info(userName + ": " + text);
|
||||
}
|
||||
|
||||
@TikTokEventHandler
|
||||
@TikTokEventObserver
|
||||
public void onGift(LiveClient liveClient, TikTokGiftEvent event) {
|
||||
var message = switch (event.getGift()) {
|
||||
case ROSE -> "Thanks :)";
|
||||
|
||||
Reference in New Issue
Block a user