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 TikTokLiveMessageHandler messageHandler;
|
||||||
private final TikTokLiveEventHandler tikTokEventHandler;
|
private final TikTokLiveEventHandler tikTokEventHandler;
|
||||||
private WebSocketClient webSocketClient;
|
private WebSocketClient webSocketClient;
|
||||||
|
|
||||||
|
private TikTokWebSocketPingingTask pingingTask;
|
||||||
private boolean isConnected;
|
private boolean isConnected;
|
||||||
|
|
||||||
public TikTokWebSocketClient(
|
public TikTokWebSocketClient(
|
||||||
@@ -51,11 +53,11 @@ public class TikTokWebSocketClient implements SocketClient {
|
|||||||
this.messageHandler = messageHandler;
|
this.messageHandler = messageHandler;
|
||||||
this.tikTokEventHandler = tikTokEventHandler;
|
this.tikTokEventHandler = tikTokEventHandler;
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
|
pingingTask = new TikTokWebSocketPingingTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient)
|
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient) {
|
||||||
{
|
|
||||||
if (isConnected) {
|
if (isConnected) {
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
@@ -74,16 +76,16 @@ public class TikTokWebSocketClient implements SocketClient {
|
|||||||
// ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
|
// ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
|
||||||
// if (proxyClientSettings.isEnabled())
|
// if (proxyClientSettings.isEnabled())
|
||||||
// connectProxy(proxyClientSettings);
|
// connectProxy(proxyClientSettings);
|
||||||
// else
|
// else
|
||||||
connectDefault();
|
connectDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectDefault() {
|
private void connectDefault() {
|
||||||
try {
|
try {
|
||||||
webSocketClient.connect();
|
webSocketClient.connect();
|
||||||
|
pingingTask.run(webSocketClient);
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
} catch (Exception e)
|
} catch (Exception e) {
|
||||||
{
|
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
throw new TikTokLiveException("Failed to connect to the websocket", e);
|
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) {
|
if (proxySettings.getType() == Proxy.Type.SOCKS) {
|
||||||
SSLContext sc = SSLContext.getInstance("SSL");
|
SSLContext sc = SSLContext.getInstance("SSL");
|
||||||
sc.init(null, new TrustManager[]{new X509TrustManager() {
|
sc.init(null, new TrustManager[]{new X509TrustManager() {
|
||||||
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {}
|
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
|
||||||
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {}
|
}
|
||||||
public X509Certificate[] getAcceptedIssuers() { return null; }
|
|
||||||
|
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public X509Certificate[] getAcceptedIssuers() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}}, null);
|
}}, null);
|
||||||
webSocketClient.setSocketFactory(sc.getSocketFactory());
|
webSocketClient.setSocketFactory(sc.getSocketFactory());
|
||||||
}
|
}
|
||||||
webSocketClient.connect();
|
webSocketClient.connect();
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e)
|
} catch (Exception e) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,6 +134,7 @@ public class TikTokWebSocketClient implements SocketClient {
|
|||||||
public void stop() {
|
public void stop() {
|
||||||
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
|
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
|
||||||
webSocketClient.closeConnection(0, "");
|
webSocketClient.closeConnection(0, "");
|
||||||
|
pingingTask.stop();
|
||||||
}
|
}
|
||||||
webSocketClient = null;
|
webSocketClient = null;
|
||||||
isConnected = false;
|
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;
|
import java.util.Map;
|
||||||
|
|
||||||
public class CollectorExample {
|
public class CollectorExample {
|
||||||
|
|
||||||
|
|
||||||
|
private static String mongoUser;
|
||||||
|
|
||||||
|
private static String mongoPassword;
|
||||||
|
|
||||||
|
private static String mongoDatabase;
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
|
|
||||||
var collector = TikTokLiveCollector.use(settings ->
|
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");
|
settings.setDatabaseName("tiktok");
|
||||||
});
|
});
|
||||||
collector.connectDatabase();
|
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";
|
var sessionTag = "Dupa";
|
||||||
for (var user : users) {
|
for (var user : users) {
|
||||||
TikTokLive.newClient(user)
|
TikTokLive.newClient(user)
|
||||||
@@ -51,10 +59,9 @@ public class CollectorExample {
|
|||||||
{
|
{
|
||||||
event.getException().printStackTrace();
|
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 false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ public class ListenerExample
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Method in TikTokEventListener should meet 4 requirements to be detected
|
* Method in TikTokEventListener should meet 4 requirements to be detected
|
||||||
* - must have @TikTokEventHandler annotation
|
* - must have @TikTokEventObserver annotation
|
||||||
* - must have 2 parameters
|
* - must have 2 parameters
|
||||||
* - first parameter must be LiveClient
|
* - first parameter must be LiveClient
|
||||||
* - second must be class that extending TikTokEvent
|
* - second must be class that extending TikTokEvent
|
||||||
|
|||||||
@@ -24,27 +24,34 @@ package io.github.jwdeveloper.tiktok;
|
|||||||
|
|
||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
|
|
||||||
public class ProxyExample
|
public class ProxyExample {
|
||||||
{
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
TikTokLive.newClient(SimpleExample.TIKTOK_HOSTNAME)
|
TikTokLive.newClient(SimpleExample.TIKTOK_HOSTNAME)
|
||||||
.configure(clientSettings -> {
|
.configure(clientSettings -> {
|
||||||
clientSettings.setPrintToConsole(true);
|
clientSettings.setPrintToConsole(true);
|
||||||
clientSettings.getHttpSettings().configureProxy(proxySettings -> {
|
clientSettings.getHttpSettings().configureProxy(proxySettings -> {
|
||||||
proxySettings.setOnProxyUpdated(proxyData -> System.err.println("Next proxy: " + proxyData.toString()));
|
proxySettings.setOnProxyUpdated(proxyData -> System.err.println("Next proxy: " + proxyData.toString()));
|
||||||
proxySettings.setType(Proxy.Type.SOCKS);
|
proxySettings.setType(Proxy.Type.SOCKS);
|
||||||
proxySettings.addProxy("localhost", 8080);
|
proxySettings.addProxy("localhost", 8080);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.onConnected((liveClient, event) ->
|
.onConnected((liveClient, event) ->
|
||||||
liveClient.getLogger().info("Connected "+liveClient.getRoomInfo().getHostName()))
|
{
|
||||||
.onDisconnected((liveClient, event) ->
|
liveClient.getLogger().info("Connected " + liveClient.getRoomInfo().getHostName());
|
||||||
liveClient.getLogger().info("Disconnect reason: "+event.getReason()))
|
})
|
||||||
.onLiveEnded((liveClient, event) ->
|
.onDisconnected((liveClient, event) ->
|
||||||
liveClient.getLogger().info("Live Ended"))
|
{
|
||||||
.onError((liveClient, event) ->
|
liveClient.getLogger().info("Disconnect reason: " + event.getReason());
|
||||||
event.getException().printStackTrace())
|
})
|
||||||
.buildAndConnect();
|
.onLiveEnded((liveClient, event) ->
|
||||||
|
{
|
||||||
|
liveClient.getLogger().info("Live Ended");
|
||||||
|
})
|
||||||
|
.onError((liveClient, event) ->
|
||||||
|
{
|
||||||
|
event.getException().printStackTrace();
|
||||||
|
})
|
||||||
|
.buildAndConnect();
|
||||||
System.in.read();
|
System.in.read();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -652,24 +652,24 @@ public static void main(String[] args) throws IOException {
|
|||||||
|
|
||||||
public static class CustomListener implements TikTokEventListener {
|
public static class CustomListener implements TikTokEventListener {
|
||||||
|
|
||||||
@TikTokEventHandler
|
@TikTokEventObserver
|
||||||
public void onLike(LiveClient liveClient, TikTokLikeEvent event) {
|
public void onLike(LiveClient liveClient, TikTokLikeEvent event) {
|
||||||
System.out.println(event.toString());
|
System.out.println(event.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@TikTokEventHandler
|
@TikTokEventObserver
|
||||||
public void onError(LiveClient liveClient, TikTokErrorEvent event) {
|
public void onError(LiveClient liveClient, TikTokErrorEvent event) {
|
||||||
// event.getException().printStackTrace();
|
// event.getException().printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
@TikTokEventHandler
|
@TikTokEventObserver
|
||||||
public void onComment(LiveClient liveClient, TikTokCommentEvent event) {
|
public void onComment(LiveClient liveClient, TikTokCommentEvent event) {
|
||||||
var userName = event.getUser().getName();
|
var userName = event.getUser().getName();
|
||||||
var text = event.getText();
|
var text = event.getText();
|
||||||
liveClient.getLogger().info(userName + ": " + text);
|
liveClient.getLogger().info(userName + ": " + text);
|
||||||
}
|
}
|
||||||
|
|
||||||
@TikTokEventHandler
|
@TikTokEventObserver
|
||||||
public void onGift(LiveClient liveClient, TikTokGiftEvent event) {
|
public void onGift(LiveClient liveClient, TikTokGiftEvent event) {
|
||||||
var message = switch (event.getGift()) {
|
var message = switch (event.getGift()) {
|
||||||
case ROSE -> "Thanks :)";
|
case ROSE -> "Thanks :)";
|
||||||
|
|||||||
Reference in New Issue
Block a user