Skip to content

Commit 2e22da1

Browse files
authored
Merge pull request #115 from jwdeveloper/develop-1.8.13
Develop 1.8.13
2 parents 125e421 + 4b4874d commit 2e22da1

File tree

4 files changed

+60
-27
lines changed

4 files changed

+60
-27
lines changed

API/src/main/java/io/github/jwdeveloper/tiktok/data/events/TikTokLinkMicBattleEvent.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastLinkMicBattle;
3131
import lombok.Getter;
3232

33+
import java.util.List;
34+
3335
/**
3436
* Triggered every time a battle starts & ends
3537
*/
@@ -73,21 +75,52 @@ public TikTokLinkMicBattleEvent(WebcastLinkMicBattle msg) {
7375
public Team1v1 get1v1Team(String battleHostName) {
7476
if (!is1v1())
7577
throw new TikTokLiveException("Teams are not instance of 1v1 battle!");
76-
if (team1.getAs1v1Team().getHost().getName().equals(battleHostName))
77-
return team1.getAs1v1Team();
78-
if (team2.getAs1v1Team().getHost().getName().equals(battleHostName))
79-
return team2.getAs1v1Team();
80-
return null;
78+
List<Team> list = getTeams(battleHostName);
79+
return list.isEmpty() ? null : list.get(0).getAs1v1Team();
8180
}
8281

8382
public Team2v2 get2v2Team(String battleHostName) {
8483
if (!is2v2())
8584
throw new TikTokLiveException("Teams are not instance of 2v2 battle!");
86-
if (team1.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
87-
return team1.getAs2v2Team();
88-
if (team2.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
89-
return team2.getAs2v2Team();
90-
return null;
85+
List<Team> list = getTeams(battleHostName);
86+
return list.isEmpty() ? null : list.get(0).getAs2v2Team();
87+
}
88+
89+
/**
90+
* @param battleHostName name of host to search
91+
* @return Team1v1 instance not containing name of host */
92+
public Team1v1 get1v1OpponentTeam(String battleHostName) {
93+
if (!is1v1())
94+
throw new TikTokLiveException("Teams are not instance of 1v1 battle!");
95+
List<Team> list = getTeams(battleHostName);
96+
return list.isEmpty() ? null : list.get(1).getAs1v1Team();
97+
}
98+
99+
public Team2v2 get2x2OpponentTeam(String battleHostName) {
100+
if (!is2v2())
101+
throw new TikTokLiveException("Teams are not instance of 2v2 battle!");
102+
List<Team> list = getTeams(battleHostName);
103+
return list.isEmpty() ? null : list.get(1).getAs2v2Team();
104+
}
105+
106+
/**
107+
* @param battleHostName name of host to search
108+
* @return {@link List<Team>} with host team first, then opponent team
109+
* <p> Empty if host is in neither otherwise always 2 in length;
110+
*/
111+
public List<Team> getTeams(String battleHostName) {
112+
if (is1v1()) {
113+
if (team1.getAs1v1Team().getHost().getName().equals(battleHostName))
114+
return List.of(team1, team2);
115+
if (team2.getAs1v1Team().getHost().getName().equals(battleHostName))
116+
return List.of(team2, team1);
117+
} else {
118+
if (team1.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
119+
return List.of(team1, team2);
120+
if (team2.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
121+
return List.of(team2, team1);
122+
}
123+
return List.of();
91124
}
92125

93126
public boolean is1v1() {

API/src/main/java/io/github/jwdeveloper/tiktok/data/settings/ProxyClientSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
@Setter
3434
public class ProxyClientSettings implements Iterator<ProxyData>, Iterable<ProxyData>
3535
{
36-
private boolean enabled, autoDiscard = true, fallback = true;
36+
private boolean enabled, autoDiscard = true, fallback = true, allowWebsocket = true;
3737
private Rotation rotation = Rotation.CONSECUTIVE;
3838
private final List<ProxyData> proxyList = new ArrayList<>();
3939
private int index;

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ public void start(LiveConnectionData.Response connectionData, LiveClient liveCli
7474
tikTokEventHandler,
7575
liveClient);
7676

77-
// ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
78-
// if (proxyClientSettings.isEnabled())
79-
// connectProxy(proxyClientSettings);
80-
// else
81-
connectDefault();
77+
ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
78+
if (proxyClientSettings.isEnabled() && proxyClientSettings.isAllowWebsocket())
79+
connectProxy(proxyClientSettings);
80+
else
81+
connectDefault();
8282
}
8383

8484
private void connectDefault() {
@@ -115,15 +115,14 @@ public X509Certificate[] getAcceptedIssuers() {
115115
}
116116
while (proxySettings.hasNext()) {
117117
ProxyData proxyData = proxySettings.next();
118-
if (!tryProxyConnection(proxySettings, proxyData)) {
119-
if (proxySettings.isAutoDiscard())
120-
proxySettings.remove();
121-
continue;
122-
}
123-
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
124-
isConnected = true;
125-
break;
126-
}
118+
if (tryProxyConnection(proxySettings, proxyData)) {
119+
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
120+
isConnected = true;
121+
break;
122+
}
123+
if (proxySettings.isAutoDiscard())
124+
proxySettings.remove();
125+
}
127126
if (!isConnected)
128127
throw new TikTokLiveException("Failed to connect to the websocket");
129128
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424

2525
import org.java_websocket.WebSocket;
2626

27+
import java.util.Base64;
28+
2729
public class WebSocketHeartbeatTask
2830
{
2931
private Thread thread;
3032
private boolean isRunning = false;
3133
private final int MAX_TIMEOUT = 250;
3234
private final int SLEEP_TIME = 500;
33-
private final byte[] heartbeatBytes = {58, 2, 104, 98}; // Byte Array of "3A026862" which is TikTok's custom heartbeat value
35+
private final byte[] heartbeatBytes = Base64.getDecoder().decode("MgJwYjoCaGI="); // Used to be '3A026862' aka ':\x02hb', now is '2\x02pb:\x02hb'.
3436

3537
public void run(WebSocket webSocket, long pingTaskTime) {
3638
stop();
@@ -58,6 +60,5 @@ private void heartbeatTask(WebSocket webSocket, long pingTaskTime) {
5860
isRunning = false;
5961
}
6062
}
61-
6263
}
6364
}

0 commit comments

Comments
 (0)