Skip to content

Commit 1a1860e

Browse files
authored
Merge pull request #46 from jwdeveloper/develop-1.0.18
MINOR 1.0.18 - fix small fixed related to proxy system - introduce recorder extension - introduce collector extension Events: TikTokConnectingEvent - triggered as a first event, when client is initializing connection TikTokRoomDataResponseEvent - returns informations about room in the JSON formmat Bugs: - Live is getting disconnect after few minutes, to fix that, PingingLoop have been run in background
2 parents 72092bb + 8a4248d commit 1a1860e

File tree

37 files changed

+1342
-38
lines changed

37 files changed

+1342
-38
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 [email protected]
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 [email protected]
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/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<dependency>
4040
<groupId>org.java-websocket</groupId>
4141
<artifactId>Java-WebSocket</artifactId>
42-
<version>1.5.4</version>
42+
<version>1.5.5</version>
4343
</dependency>
4444
<dependency>
4545
<groupId>org.testng</groupId>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public static boolean isLiveOnline(String hostName)
5050
return requests().fetchLiveUserData(hostName).isLiveOnline();
5151
}
5252

53-
5453
/**
5554
*
5655
* @param hostName profile name of Tiktok user could be found in profile link

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

Lines changed: 5 additions & 2 deletions
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
}
@@ -159,8 +162,8 @@ public void disconnect() {
159162
if (liveRoomInfo.hasConnectionState(ConnectionState.DISCONNECTED)) {
160163
return;
161164
}
162-
webSocketClient.stop();
163165
setState(ConnectionState.DISCONNECTED);
166+
webSocketClient.stop();
164167
}
165168

166169
private void setState(ConnectionState connectionState) {

Client/src/main/java/io/github/jwdeveloper/tiktok/http/HttpProxyClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {}
9999
try {
100100
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxySettings.next().toSocketAddress());
101101

102-
System.err.println("Connecting to "+ url);
103102
HttpsURLConnection socksConnection = (HttpsURLConnection) url.openConnection(proxy);
104103
socksConnection.setSSLSocketFactory(sc.getSocketFactory());
105104
socksConnection.setConnectTimeout(httpClientSettings.getTimeout().toMillisPart());

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: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
*/
2323
package io.github.jwdeveloper.tiktok.websocket;
2424

25-
2625
import io.github.jwdeveloper.tiktok.*;
2726
import io.github.jwdeveloper.tiktok.data.dto.ProxyData;
2827
import io.github.jwdeveloper.tiktok.data.requests.LiveConnectionData;
@@ -41,6 +40,8 @@ public class TikTokWebSocketClient implements SocketClient {
4140
private final TikTokLiveMessageHandler messageHandler;
4241
private final TikTokLiveEventHandler tikTokEventHandler;
4342
private WebSocketClient webSocketClient;
43+
44+
private TikTokWebSocketPingingTask pingingTask;
4445
private boolean isConnected;
4546

4647
public TikTokWebSocketClient(
@@ -51,11 +52,11 @@ public TikTokWebSocketClient(
5152
this.messageHandler = messageHandler;
5253
this.tikTokEventHandler = tikTokEventHandler;
5354
isConnected = false;
55+
pingingTask = new TikTokWebSocketPingingTask();
5456
}
5557

5658
@Override
57-
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient)
58-
{
59+
public void start(LiveConnectionData.Response connectionData, LiveClient liveClient) {
5960
if (isConnected) {
6061
stop();
6162
}
@@ -74,16 +75,16 @@ public void start(LiveConnectionData.Response connectionData, LiveClient liveCli
7475
// ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
7576
// if (proxyClientSettings.isEnabled())
7677
// connectProxy(proxyClientSettings);
77-
// else
78-
connectDefault();
78+
// else
79+
connectDefault();
7980
}
8081

8182
private void connectDefault() {
8283
try {
8384
webSocketClient.connect();
85+
pingingTask.run(webSocketClient);
8486
isConnected = true;
85-
} catch (Exception e)
86-
{
87+
} catch (Exception e) {
8788
isConnected = false;
8889
throw new TikTokLiveException("Failed to connect to the websocket", e);
8990
}
@@ -105,28 +106,34 @@ public void connectProxy(ProxyClientSettings proxySettings) {
105106
}
106107

107108
public boolean tryProxyConnection(ProxyClientSettings proxySettings, ProxyData proxyData) {
108-
webSocketClient.setProxy(new Proxy(proxySettings.getType(), proxyData.toSocketAddress()));
109109
try {
110110
if (proxySettings.getType() == Proxy.Type.SOCKS) {
111111
SSLContext sc = SSLContext.getInstance("SSL");
112112
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; }
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+
}
116122
}}, null);
117123
webSocketClient.setSocketFactory(sc.getSocketFactory());
118124
}
125+
webSocketClient.setProxy(new Proxy(proxySettings.getType(), proxyData.toSocketAddress()));
119126
webSocketClient.connect();
120127
return true;
121-
} catch (Exception e)
122-
{
128+
} catch (Exception e) {
123129
return false;
124130
}
125131
}
126132

127133
public void stop() {
128134
if (isConnected && webSocketClient != null && webSocketClient.isOpen()) {
129135
webSocketClient.closeConnection(0, "");
136+
pingingTask.stop();
130137
}
131138
webSocketClient = null;
132139
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+
}

0 commit comments

Comments
 (0)