Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import io.github.jwdeveloper.tiktok.messages.webcast.WebcastLinkMicBattle;
import lombok.Getter;

import java.util.List;

/**
* Triggered every time a battle starts & ends
*/
Expand Down Expand Up @@ -73,21 +75,52 @@ public TikTokLinkMicBattleEvent(WebcastLinkMicBattle msg) {
public Team1v1 get1v1Team(String battleHostName) {
if (!is1v1())
throw new TikTokLiveException("Teams are not instance of 1v1 battle!");
if (team1.getAs1v1Team().getHost().getName().equals(battleHostName))
return team1.getAs1v1Team();
if (team2.getAs1v1Team().getHost().getName().equals(battleHostName))
return team2.getAs1v1Team();
return null;
List<Team> list = getTeams(battleHostName);
return list.isEmpty() ? null : list.get(0).getAs1v1Team();
}

public Team2v2 get2v2Team(String battleHostName) {
if (!is2v2())
throw new TikTokLiveException("Teams are not instance of 2v2 battle!");
if (team1.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
return team1.getAs2v2Team();
if (team2.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
return team2.getAs2v2Team();
return null;
List<Team> list = getTeams(battleHostName);
return list.isEmpty() ? null : list.get(0).getAs2v2Team();
}

/**
* @param battleHostName name of host to search
* @return Team1v1 instance not containing name of host */
public Team1v1 get1v1OpponentTeam(String battleHostName) {
if (!is1v1())
throw new TikTokLiveException("Teams are not instance of 1v1 battle!");
List<Team> list = getTeams(battleHostName);
return list.isEmpty() ? null : list.get(1).getAs1v1Team();
}

public Team2v2 get2x2OpponentTeam(String battleHostName) {
if (!is2v2())
throw new TikTokLiveException("Teams are not instance of 2v2 battle!");
List<Team> list = getTeams(battleHostName);
return list.isEmpty() ? null : list.get(1).getAs2v2Team();
}

/**
* @param battleHostName name of host to search
* @return {@link List<Team>} with host team first, then opponent team
* <p> Empty if host is in neither otherwise always 2 in length;
*/
public List<Team> getTeams(String battleHostName) {
if (is1v1()) {
if (team1.getAs1v1Team().getHost().getName().equals(battleHostName))
return List.of(team1, team2);
if (team2.getAs1v1Team().getHost().getName().equals(battleHostName))
return List.of(team2, team1);
} else {
if (team1.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
return List.of(team1, team2);
if (team2.getAs2v2Team().getHosts().stream().anyMatch(user -> user.getName().equals(battleHostName)))
return List.of(team2, team1);
}
return List.of();
}

public boolean is1v1() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@Setter
public class ProxyClientSettings implements Iterator<ProxyData>, Iterable<ProxyData>
{
private boolean enabled, autoDiscard = true, fallback = true;
private boolean enabled, autoDiscard = true, fallback = true, allowWebsocket = true;
private Rotation rotation = Rotation.CONSECUTIVE;
private final List<ProxyData> proxyList = new ArrayList<>();
private int index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public void start(LiveConnectionData.Response connectionData, LiveClient liveCli
tikTokEventHandler,
liveClient);

// ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
// if (proxyClientSettings.isEnabled())
// connectProxy(proxyClientSettings);
// else
connectDefault();
ProxyClientSettings proxyClientSettings = clientSettings.getHttpSettings().getProxyClientSettings();
if (proxyClientSettings.isEnabled() && proxyClientSettings.isAllowWebsocket())
connectProxy(proxyClientSettings);
else
connectDefault();
}

private void connectDefault() {
Expand Down Expand Up @@ -115,15 +115,14 @@ public X509Certificate[] getAcceptedIssuers() {
}
while (proxySettings.hasNext()) {
ProxyData proxyData = proxySettings.next();
if (!tryProxyConnection(proxySettings, proxyData)) {
if (proxySettings.isAutoDiscard())
proxySettings.remove();
continue;
}
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
isConnected = true;
break;
}
if (tryProxyConnection(proxySettings, proxyData)) {
heartbeatTask.run(webSocketClient, clientSettings.getPingInterval());
isConnected = true;
break;
}
if (proxySettings.isAutoDiscard())
proxySettings.remove();
}
if (!isConnected)
throw new TikTokLiveException("Failed to connect to the websocket");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

import org.java_websocket.WebSocket;

import java.util.Base64;

public class WebSocketHeartbeatTask
{
private Thread thread;
private boolean isRunning = false;
private final int MAX_TIMEOUT = 250;
private final int SLEEP_TIME = 500;
private final byte[] heartbeatBytes = {58, 2, 104, 98}; // Byte Array of "3A026862" which is TikTok's custom heartbeat value
private final byte[] heartbeatBytes = Base64.getDecoder().decode("MgJwYjoCaGI="); // Used to be '3A026862' aka ':\x02hb', now is '2\x02pb:\x02hb'.

public void run(WebSocket webSocket, long pingTaskTime) {
stop();
Expand Down Expand Up @@ -58,6 +60,5 @@ private void heartbeatTask(WebSocket webSocket, long pingTaskTime) {
isRunning = false;
}
}

}
}