Skip to content

Commit 9ddec45

Browse files
committed
Including Pinging Task
1 parent a805844 commit 9ddec45

File tree

6 files changed

+122
-40
lines changed

6 files changed

+122
-40
lines changed

Client/src/main/java/io/github/jwdeveloper/tiktok/websocket/TikTokWebSocketClient.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class TikTokWebSocketClient implements SocketClient {
4141
private final TikTokLiveMessageHandler messageHandler;
4242
private final TikTokLiveEventHandler tikTokEventHandler;
4343
private WebSocketClient webSocketClient;
44+
45+
private TikTokWebSocketPingingTask pingingTask;
4446
private boolean isConnected;
4547

4648
public TikTokWebSocketClient(
@@ -51,11 +53,11 @@ public TikTokWebSocketClient(
5153
this.messageHandler = messageHandler;
5254
this.tikTokEventHandler = tikTokEventHandler;
5355
isConnected = false;
56+
pingingTask = new TikTokWebSocketPingingTask();
5457
}
5558

5659
@Override
57-
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient)
58-
{
60+
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient) {
5961
if (isConnected) {
6062
stop();
6163
}
@@ -74,16 +76,16 @@ public void start(LiveConnectionData.Response connectionData, LiveClient liveCli
7476
// ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
7577
// if (proxyClientSettings.isEnabled())
7678
// connectProxy(proxyClientSettings);
77-
// else
78-
connectDefault();
79+
// else
80+
connectDefault();
7981
}
8082

8183
private void connectDefault() {
8284
try {
8385
webSocketClient.connect();
86+
pingingTask.run(webSocketClient);
8487
isConnected = true;
85-
} catch (Exception e)
86-
{
88+
} catch (Exception e) {
8789
isConnected = false;
8890
throw new TikTokLiveException("Failed to connect to the websocket", e);
8991
}
@@ -110,23 +112,29 @@ public boolean tryProxyConnection(ProxyClientSettings proxySettings, ProxyData p
110112
if (proxySettings.getType() == Proxy.Type.SOCKS) {
111113
SSLContext sc = SSLContext.getInstance("SSL");
112114
sc.init(null, new TrustManager[]{new X509TrustManager() {
113-
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {}
114-
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {}
115-
public X509Certificate[] getAcceptedIssuers() { return null; }
115+
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
116+
}
117+
118+
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
119+
}
120+
121+
public X509Certificate[] getAcceptedIssuers() {
122+
return null;
123+
}
116124
}}, null);
117125
webSocketClient.setSocketFactory(sc.getSocketFactory());
118126
}
119127
webSocketClient.connect();
120128
return true;
121-
} catch (Exception e)
122-
{
129+
} catch (Exception e) {
123130
return false;
124131
}
125132
}
126133

127134
public void stop() {
128135
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
129136
webSocketClient.closeConnection(0, "");
137+
pingingTask.stop();
130138
}
131139
webSocketClient = null;
132140
isConnected = false;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.github.jwdeveloper.tiktok.websocket;
2+
3+
import org.java_websocket.WebSocket;
4+
5+
import java.util.Random;
6+
7+
public class TikTokWebSocketPingingTask
8+
{
9+
private Thread thread;
10+
private boolean isRunning = false;
11+
private final int MIN_TIMEOUT = 250;
12+
private final int MAX_TIMEOUT = 500;
13+
14+
15+
public void run(WebSocket webSocket)
16+
{
17+
stop();
18+
thread = new Thread(() ->
19+
{
20+
pingTask(webSocket);
21+
});
22+
isRunning =true;
23+
thread.start();
24+
}
25+
26+
public void stop()
27+
{
28+
if(thread != null)
29+
{
30+
thread.interrupt();
31+
}
32+
isRunning = false;
33+
}
34+
35+
36+
private void pingTask(WebSocket webSocket)
37+
{
38+
var random = new Random();
39+
while (isRunning)
40+
{
41+
try
42+
{
43+
if(!webSocket.isOpen())
44+
{
45+
Thread.sleep(100);
46+
continue;
47+
}
48+
webSocket.sendPing();
49+
50+
var timeout = random.nextInt(MAX_TIMEOUT)+MIN_TIMEOUT;
51+
Thread.sleep(timeout);
52+
}
53+
catch (Exception e)
54+
{
55+
isRunning = false;
56+
}
57+
}
58+
59+
}
60+
}

Examples/src/main/java/io/github/jwdeveloper/tiktok/CollectorExample.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,24 @@
3030
import java.util.Map;
3131

3232
public class CollectorExample {
33+
34+
35+
private static String mongoUser;
36+
37+
private static String mongoPassword;
38+
39+
private static String mongoDatabase;
40+
3341
public static void main(String[] args) throws IOException {
3442

3543
var collector = TikTokLiveCollector.use(settings ->
3644
{
37-
settings.setConnectionUrl("mongodb+srv://jwoln:qaz123456@jwdatabase.a15gw.mongodb.net/?retryWrites=true&w=majority");
45+
settings.setConnectionUrl("mongodb+srv://" + mongoUser + ":" + mongoPassword + "@" + mongoDatabase + "/?retryWrites=true&w=majority");
3846
settings.setDatabaseName("tiktok");
3947
});
4048
collector.connectDatabase();
4149

42-
var users = List.of("tehila_723", "dino123597", "domaxyzx", "dash4214","obserwacje_live");
50+
var users = List.of("tehila_723", "dino123597", "domaxyzx", "dash4214", "obserwacje_live");
4351
var sessionTag = "Dupa";
4452
for (var user : users) {
4553
TikTokLive.newClient(user)
@@ -51,10 +59,9 @@ public static void main(String[] args) throws IOException {
5159
{
5260
event.getException().printStackTrace();
5361
})
54-
.addListener(collector.newListener(Map.of("sessionTag", sessionTag),document ->
62+
.addListener(collector.newListener(Map.of("sessionTag", sessionTag), document ->
5563
{
56-
if(document.get("dataType") == "message")
57-
{
64+
if (document.get("dataType") == "message") {
5865
return false;
5966
}
6067
return true;

Examples/src/main/java/io/github/jwdeveloper/tiktok/ListenerExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static void main(String[] args) throws IOException {
5757
/**
5858
*
5959
* Method in TikTokEventListener should meet 4 requirements to be detected
60-
* - must have @TikTokEventHandler annotation
60+
* - must have @TikTokEventObserver annotation
6161
* - must have 2 parameters
6262
* - first parameter must be LiveClient
6363
* - second must be class that extending TikTokEvent

Examples/src/main/java/io/github/jwdeveloper/tiktok/ProxyExample.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,34 @@
2424

2525
import java.net.Proxy;
2626

27-
public class ProxyExample
28-
{
27+
public class ProxyExample {
2928
public static void main(String[] args) throws Exception {
3029
TikTokLive.newClient(SimpleExample.TIKTOK_HOSTNAME)
31-
.configure(clientSettings -> {
32-
clientSettings.setPrintToConsole(true);
33-
clientSettings.getHttpSettings().configureProxy(proxySettings -> {
34-
proxySettings.setOnProxyUpdated(proxyData -> System.err.println("Next proxy: " + proxyData.toString()));
35-
proxySettings.setType(Proxy.Type.SOCKS);
36-
proxySettings.addProxy("localhost", 8080);
37-
});
38-
})
39-
.onConnected((liveClient, event) ->
40-
liveClient.getLogger().info("Connected "+liveClient.getRoomInfo().getHostName()))
41-
.onDisconnected((liveClient, event) ->
42-
liveClient.getLogger().info("Disconnect reason: "+event.getReason()))
43-
.onLiveEnded((liveClient, event) ->
44-
liveClient.getLogger().info("Live Ended"))
45-
.onError((liveClient, event) ->
46-
event.getException().printStackTrace())
47-
.buildAndConnect();
30+
.configure(clientSettings -> {
31+
clientSettings.setPrintToConsole(true);
32+
clientSettings.getHttpSettings().configureProxy(proxySettings -> {
33+
proxySettings.setOnProxyUpdated(proxyData -> System.err.println("Next proxy: " + proxyData.toString()));
34+
proxySettings.setType(Proxy.Type.SOCKS);
35+
proxySettings.addProxy("localhost", 8080);
36+
});
37+
})
38+
.onConnected((liveClient, event) ->
39+
{
40+
liveClient.getLogger().info("Connected " + liveClient.getRoomInfo().getHostName());
41+
})
42+
.onDisconnected((liveClient, event) ->
43+
{
44+
liveClient.getLogger().info("Disconnect reason: " + event.getReason());
45+
})
46+
.onLiveEnded((liveClient, event) ->
47+
{
48+
liveClient.getLogger().info("Live Ended");
49+
})
50+
.onError((liveClient, event) ->
51+
{
52+
event.getException().printStackTrace();
53+
})
54+
.buildAndConnect();
4855
System.in.read();
4956
}
5057
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -652,24 +652,24 @@ public static void main(String[] args) throws IOException {
652652

653653
public static class CustomListener implements TikTokEventListener {
654654

655-
@TikTokEventHandler
655+
@TikTokEventObserver
656656
public void onLike(LiveClient liveClient, TikTokLikeEvent event) {
657657
System.out.println(event.toString());
658658
}
659659

660-
@TikTokEventHandler
660+
@TikTokEventObserver
661661
public void onError(LiveClient liveClient, TikTokErrorEvent event) {
662662
// event.getException().printStackTrace();
663663
}
664664

665-
@TikTokEventHandler
665+
@TikTokEventObserver
666666
public void onComment(LiveClient liveClient, TikTokCommentEvent event) {
667667
var userName = event.getUser().getName();
668668
var text = event.getText();
669669
liveClient.getLogger().info(userName + ": " + text);
670670
}
671671

672-
@TikTokEventHandler
672+
@TikTokEventObserver
673673
public void onGift(LiveClient liveClient, TikTokGiftEvent event) {
674674
var message = switch (event.getGift()) {
675675
case ROSE -> "Thanks :)";

0 commit comments

Comments
 (0)