Skip to content

Commit ff5310f

Browse files
authored
Merge branch 'develop-1.0.18' into fixes-updates
2 parents 8a7b9e8 + 9ddec45 commit ff5310f

File tree

33 files changed

+1333
-19
lines changed

33 files changed

+1333
-19
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
package io.github.jwdeveloper.tiktok.data.events.control;
24+
25+
import io.github.jwdeveloper.tiktok.annotations.EventMeta;
26+
import io.github.jwdeveloper.tiktok.annotations.EventType;
27+
import io.github.jwdeveloper.tiktok.data.events.common.TikTokLiveClientEvent;
28+
29+
30+
/**
31+
* Triggered when client is connecting to live is successfully established.
32+
*/
33+
@EventMeta(eventType = EventType.Control)
34+
public class TikTokConnectingEvent extends TikTokLiveClientEvent
35+
{
36+
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
package io.github.jwdeveloper.tiktok.data.events.http;
24+
25+
import io.github.jwdeveloper.tiktok.annotations.EventMeta;
26+
import io.github.jwdeveloper.tiktok.annotations.EventType;
27+
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
28+
import io.github.jwdeveloper.tiktok.data.requests.LiveData;
29+
import lombok.AllArgsConstructor;
30+
import lombok.Getter;
31+
32+
@Getter
33+
@AllArgsConstructor
34+
@EventMeta(eventType = EventType.Debug)
35+
public class TikTokRoomDataResponseEvent extends TikTokEvent
36+
{
37+
private final LiveData.Response liveData;
38+
}

Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import io.github.jwdeveloper.tiktok.data.events.TikTokErrorEvent;
2727
import io.github.jwdeveloper.tiktok.data.events.TikTokReconnectingEvent;
2828
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
29+
import io.github.jwdeveloper.tiktok.data.events.control.TikTokConnectingEvent;
30+
import io.github.jwdeveloper.tiktok.data.events.http.TikTokRoomDataResponseEvent;
2931
import io.github.jwdeveloper.tiktok.data.events.room.TikTokRoomInfoEvent;
3032
import io.github.jwdeveloper.tiktok.data.requests.LiveConnectionData;
3133
import io.github.jwdeveloper.tiktok.data.requests.LiveData;
@@ -120,7 +122,7 @@ public void tryConnect() {
120122
}
121123

122124
setState(ConnectionState.CONNECTING);
123-
125+
tikTokEventHandler.publish(this,new TikTokConnectingEvent());
124126
var userDataRequest = new LiveUserData.Request(liveRoomInfo.getHostName());
125127
var userData = httpClient.fetchLiveUserData(userDataRequest);
126128
liveRoomInfo.setStartTime(userData.getStartedAtTimeStamp());
@@ -134,6 +136,7 @@ public void tryConnect() {
134136

135137
var liveDataRequest = new LiveData.Request(userData.getRoomId());
136138
var liveData = httpClient.fetchLiveData(liveDataRequest);
139+
tikTokEventHandler.publish(this, new TikTokRoomDataResponseEvent(liveData));
137140
if (liveData.getLiveStatus() == LiveData.LiveStatus.HostNotFound) {
138141
throw new TikTokLiveOfflineHostException("LiveStream for Host name could not be found.");
139142
}

Client/src/main/java/io/github/jwdeveloper/tiktok/http/mappers/LiveDataMapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class LiveDataMapper {
4343
public LiveData.Response map(String json) {
4444
var response = new LiveData.Response();
4545

46+
response.setJson(json);
4647

4748
var parsedJson = JsonParser.parseString(json);
4849
var jsonObject = parsedJson.getAsJsonObject();

Client/src/main/java/io/github/jwdeveloper/tiktok/listener/TikTokListenersManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ private ListenerBindingModel bindToEvents(TikTokEventListener listener) {
111111
EventConsumer eventMethodRef = (liveClient, event) ->
112112
{
113113
try {
114+
method.setAccessible(true);
114115
method.invoke(listener, liveClient, event);
115116
} catch (Exception e) {
116117
throw new TikTokEventListenerMethodException(e);

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
@@ -40,6 +40,8 @@ public class TikTokWebSocketClient implements SocketClient {
4040
private final TikTokLiveMessageHandler messageHandler;
4141
private final TikTokLiveEventHandler tikTokEventHandler;
4242
private WebSocketClient webSocketClient;
43+
44+
private TikTokWebSocketPingingTask pingingTask;
4345
private boolean isConnected;
4446

4547
public TikTokWebSocketClient(
@@ -50,11 +52,11 @@ public TikTokWebSocketClient(
5052
this.messageHandler = messageHandler;
5153
this.tikTokEventHandler = tikTokEventHandler;
5254
isConnected = false;
55+
pingingTask = new TikTokWebSocketPingingTask();
5356
}
5457

5558
@Override
56-
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient)
57-
{
59+
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient) {
5860
if (isConnected) {
5961
stop();
6062
}
@@ -73,16 +75,16 @@ public void start(LiveConnectionData.Response connectionData, LiveClient liveCli
7375
// ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
7476
// if (proxyClientSettings.isEnabled())
7577
// connectProxy(proxyClientSettings);
76-
// else
77-
connectDefault();
78+
// else
79+
connectDefault();
7880
}
7981

8082
private void connectDefault() {
8183
try {
8284
webSocketClient.connect();
85+
pingingTask.run(webSocketClient);
8386
isConnected = true;
84-
} catch (Exception e)
85-
{
87+
} catch (Exception e) {
8688
isConnected = false;
8789
throw new TikTokLiveException("Failed to connect to the websocket", e);
8890
}
@@ -108,24 +110,30 @@ public boolean tryProxyConnection(ProxyClientSettings proxySettings, ProxyData p
108110
if (proxySettings.getType() == Proxy.Type.SOCKS) {
109111
SSLContext sc = SSLContext.getInstance("SSL");
110112
sc.init(null, new TrustManager[]{new X509TrustManager() {
111-
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {}
112-
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {}
113-
public X509Certificate[] getAcceptedIssuers() { return null; }
113+
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
114+
}
115+
116+
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
117+
}
118+
119+
public X509Certificate[] getAcceptedIssuers() {
120+
return null;
121+
}
114122
}}, null);
115123
webSocketClient.setSocketFactory(sc.getSocketFactory());
116124
}
117125
webSocketClient.setProxy(new Proxy(proxySettings.getType(), proxyData.toSocketAddress()));
118126
webSocketClient.connect();
119127
return true;
120-
} catch (Exception e)
121-
{
128+
} catch (Exception e) {
122129
return false;
123130
}
124131
}
125132

126133
public void stop() {
127134
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
128135
webSocketClient.closeConnection(0, "");
136+
pingingTask.stop();
129137
}
130138
webSocketClient = null;
131139
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/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@
6060
<version>${project.version}</version>
6161
<scope>compile</scope>
6262
</dependency>
63+
<dependency>
64+
<groupId>io.github.jwdeveloper.tiktok</groupId>
65+
<artifactId>extension-collector</artifactId>
66+
<version>1.0.14-Release</version>
67+
<scope>compile</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>io.github.jwdeveloper.tiktok</groupId>
71+
<artifactId>extension-recorder</artifactId>
72+
<version>1.0.14-Release</version>
73+
<scope>compile</scope>
74+
</dependency>
6375
</dependencies>
6476

6577
<properties>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
package io.github.jwdeveloper.tiktok;
24+
25+
import io.github.jwdeveloper.tiktok.extension.collector.TikTokLiveCollector;
26+
27+
28+
import java.io.IOException;
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
public class CollectorExample {
33+
34+
35+
private static String mongoUser;
36+
37+
private static String mongoPassword;
38+
39+
private static String mongoDatabase;
40+
41+
public static void main(String[] args) throws IOException {
42+
43+
var collector = TikTokLiveCollector.use(settings ->
44+
{
45+
settings.setConnectionUrl("mongodb+srv://" + mongoUser + ":" + mongoPassword + "@" + mongoDatabase + "/?retryWrites=true&w=majority");
46+
settings.setDatabaseName("tiktok");
47+
});
48+
collector.connectDatabase();
49+
50+
var users = List.of("tehila_723", "dino123597", "domaxyzx", "dash4214", "obserwacje_live");
51+
var sessionTag = "Dupa";
52+
for (var user : users) {
53+
TikTokLive.newClient(user)
54+
.configure(liveClientSettings ->
55+
{
56+
liveClientSettings.setPrintToConsole(true);
57+
})
58+
.onError((liveClient, event) ->
59+
{
60+
event.getException().printStackTrace();
61+
})
62+
.addListener(collector.newListener(Map.of("sessionTag", sessionTag), document ->
63+
{
64+
if (document.get("dataType") == "message") {
65+
return false;
66+
}
67+
return true;
68+
}))
69+
.buildAndConnectAsync();
70+
}
71+
72+
System.in.read();
73+
collector.disconnectDatabase();
74+
}
75+
}

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

0 commit comments

Comments
 (0)