mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
Improve addProxy(String addressPort) in ProxyClientSettings
Added @param action description and return values Optimized methods in TikTokLiveMapper Optimized Thread in TikTokWebSocketPingingTask Optimized TikTokLiveEventHandler and TikTokLiveMessageHandler methods
This commit is contained in:
@@ -41,7 +41,7 @@ public class ProxyClientSettings implements Iterator<ProxyData>, Iterable<ProxyD
|
||||
private Consumer<ProxyData> onProxyUpdated = x -> {};
|
||||
|
||||
public boolean addProxy(String addressPort) {
|
||||
return proxyList.add(ProxyData.map(addressPort));
|
||||
return addProxy(ProxyData.map(addressPort).toSocketAddress());
|
||||
}
|
||||
|
||||
public boolean addProxy(String address, int port) {
|
||||
@@ -117,7 +117,7 @@ public class ProxyClientSettings implements Iterator<ProxyData>, Iterable<ProxyD
|
||||
}
|
||||
|
||||
/**
|
||||
* With Iterable<?> interface you can use this object inside For loop!
|
||||
* With {@code Iterable<ProxyData>} interface, you can use this object inside for loop!
|
||||
*/
|
||||
@Override
|
||||
public Iterator<ProxyData> iterator() {
|
||||
|
||||
@@ -52,77 +52,70 @@ public interface EventsBuilder<T> {
|
||||
/**
|
||||
* Invoked whenever any event is triggered
|
||||
*
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onEvent(EventConsumer<TikTokEvent> action);
|
||||
|
||||
/**
|
||||
* Invoked when information about room (live) got updated such as viewer count, etc..
|
||||
*
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onRoomInfo(EventConsumer<TikTokRoomInfoEvent> action);
|
||||
|
||||
/**
|
||||
* Invoked when someone send message to chat
|
||||
*
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onComment(EventConsumer<TikTokCommentEvent> action);
|
||||
|
||||
|
||||
/**
|
||||
* Invoked when TikTokLiveJava makes http request and getting response
|
||||
*
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onHttpResponse(EventConsumer<TikTokHttpResponseEvent> action);
|
||||
|
||||
/**
|
||||
* Invoked when TikTok protocolBuffer data "message" was successfully mapped to event
|
||||
* events contains protocol-buffer "Message" and TikTokLiveJava "Event"
|
||||
*
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onWebsocketMessage(EventConsumer<TikTokWebsocketMessageEvent> action);
|
||||
|
||||
/**
|
||||
* Invoked when there was not found event mapper for TikTok protocolBuffer data "message"
|
||||
*
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onWebsocketUnhandledMessage(EventConsumer<TikTokWebsocketUnhandledMessageEvent> action);
|
||||
|
||||
/**
|
||||
* Invoked every time TikTok sends protocolBuffer data to websocket
|
||||
* Response contains list of messages that are later mapped to events
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onWebsocketResponse(EventConsumer<TikTokWebsocketResponseEvent> action);
|
||||
|
||||
|
||||
/**
|
||||
* Invoked for gifts that has no combo, or when combo finishes
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onGift(EventConsumer<TikTokGiftEvent> action);
|
||||
|
||||
/**
|
||||
* Invoked for gifts that has combo options such as roses
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onGiftCombo(EventConsumer<TikTokGiftComboEvent> action);
|
||||
|
||||
|
||||
T onQuestion(EventConsumer<TikTokQuestionEvent> action);
|
||||
|
||||
T onSubscribe(EventConsumer<TikTokSubscribeEvent> action);
|
||||
@@ -145,36 +138,36 @@ public interface EventsBuilder<T> {
|
||||
|
||||
/**
|
||||
* Invoked when client has been successfully connected to live
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onConnected(EventConsumer<TikTokConnectedEvent> action);
|
||||
|
||||
/**
|
||||
* Invoked before client has been successfully connected to live
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onPreConnection(EventConsumer<TikTokPreConnectionEvent> action);
|
||||
|
||||
/**
|
||||
* Invoked when client tries to reconnect
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onReconnecting(EventConsumer<TikTokReconnectingEvent> action);
|
||||
|
||||
/**
|
||||
* Invoked when client disconnected
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onDisconnected(EventConsumer<TikTokDisconnectedEvent> action);
|
||||
|
||||
/**
|
||||
* Invoked when exception was throed inside client or event handler
|
||||
* @param action
|
||||
* @return
|
||||
* @param action consumable action
|
||||
* @return self instance
|
||||
*/
|
||||
T onError(EventConsumer<TikTokErrorEvent> action);
|
||||
|
||||
|
||||
@@ -148,7 +148,7 @@ public class TikTokLiveClientBuilder implements LiveClientBuilder {
|
||||
dependance.registerSingleton(LiveHttpClient.class, TikTokLiveHttpClient.class);
|
||||
}
|
||||
|
||||
/** TODO in future, custom proxy implementation that can be provided via builder
|
||||
/* TODO in future, custom proxy implementation that can be provided via builder
|
||||
* if(customProxy != null)
|
||||
* dependance.registerSingleton(TikTokProxyProvider.class,customProxy);
|
||||
* else
|
||||
|
||||
@@ -26,10 +26,7 @@ import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.live.builder.EventConsumer;
|
||||
import io.github.jwdeveloper.tiktok.live.LiveClient;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
public class TikTokLiveEventHandler {
|
||||
private final Map<Class<?>, Set<EventConsumer>> events;
|
||||
@@ -39,16 +36,8 @@ public class TikTokLiveEventHandler {
|
||||
}
|
||||
|
||||
public void publish(LiveClient tikTokLiveClient, TikTokEvent tikTokEvent) {
|
||||
if (events.containsKey(TikTokEvent.class)) {
|
||||
var handlers = events.get(TikTokEvent.class);
|
||||
handlers.forEach(handler -> handler.onEvent(tikTokLiveClient,tikTokEvent));
|
||||
}
|
||||
|
||||
if (!events.containsKey(tikTokEvent.getClass())) {
|
||||
return;
|
||||
}
|
||||
var handlers = events.get(tikTokEvent.getClass());
|
||||
handlers.forEach(handler -> handler.onEvent(tikTokLiveClient,tikTokEvent));
|
||||
Optional.ofNullable(events.get(TikTokEvent.class)).ifPresent(handlers -> handlers.forEach(handler -> handler.onEvent(tikTokLiveClient, tikTokEvent)));
|
||||
Optional.ofNullable(events.get(tikTokEvent.getClass())).ifPresent(handlers -> handlers.forEach(handler -> handler.onEvent(tikTokLiveClient, tikTokEvent)));
|
||||
}
|
||||
|
||||
public <T extends TikTokEvent> void subscribe(Class<?> clazz, EventConsumer<T> event) {
|
||||
@@ -60,21 +49,11 @@ public class TikTokLiveEventHandler {
|
||||
}
|
||||
|
||||
public <T extends TikTokEvent> void unsubscribe(EventConsumer<T> consumer) {
|
||||
for (var entry : events.entrySet()) {
|
||||
entry.getValue().remove(consumer);
|
||||
}
|
||||
events.forEach((key, value) -> value.remove(consumer));
|
||||
}
|
||||
|
||||
public <T extends TikTokEvent> void unsubscribe(Class<?> clazz, EventConsumer<T> consumer) {
|
||||
if (clazz == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!events.containsKey(clazz)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var eventSet = events.get(clazz);
|
||||
eventSet.remove(consumer);
|
||||
if (clazz != null)
|
||||
Optional.ofNullable(events.get(clazz)).ifPresent(eventConsumers -> eventConsumers.remove(consumer));
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public class TikTokLiveMessageHandler {
|
||||
|
||||
public TikTokLiveMessageHandler(TikTokLiveEventHandler tikTokEventHandler, TikTokMapper mapper) {
|
||||
this.tikTokEventHandler = tikTokEventHandler;
|
||||
this.mapper = (TikTokLiveMapper)mapper;
|
||||
this.mapper = (TikTokLiveMapper) mapper;
|
||||
}
|
||||
|
||||
public void handle(LiveClient client, WebcastResponse webcastResponse) {
|
||||
@@ -62,10 +62,9 @@ public class TikTokLiveMessageHandler {
|
||||
public void handleSingleMessage(LiveClient client, WebcastResponse.Message message)
|
||||
{
|
||||
var messageClassName = message.getMethod();
|
||||
if (!mapper.isRegistered(messageClassName)) {
|
||||
if (!mapper.isRegistered(messageClassName))
|
||||
tikTokEventHandler.publish(client, new TikTokWebsocketUnhandledMessageEvent(message));
|
||||
return;
|
||||
}
|
||||
else {
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.start();
|
||||
var events = mapper.handleMapping(messageClassName, message.getPayload().toByteArray());
|
||||
@@ -77,5 +76,5 @@ public class TikTokLiveMessageHandler {
|
||||
tikTokEventHandler.publish(client, event);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -23,14 +23,10 @@
|
||||
package io.github.jwdeveloper.tiktok.mappers;
|
||||
|
||||
import com.google.protobuf.GeneratedMessageV3;
|
||||
import io.github.jwdeveloper.tiktok.TikTokLive;
|
||||
import io.github.jwdeveloper.tiktok.data.events.common.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.mappers.data.MappingAction;
|
||||
import io.github.jwdeveloper.tiktok.mappers.data.MappingResult;
|
||||
import io.github.jwdeveloper.tiktok.mappers.data.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TikTokLiveMapper implements TikTokMapper {
|
||||
@@ -47,11 +43,7 @@ public class TikTokLiveMapper implements TikTokMapper {
|
||||
|
||||
@Override
|
||||
public TikTokMapperModel forMessage(String messageName) {
|
||||
if (!isRegistered(messageName)) {
|
||||
var model = new TikTokLiveMapperModel(messageName);
|
||||
mappers.put(messageName, model);
|
||||
}
|
||||
return mappers.get(messageName);
|
||||
return mappers.computeIfAbsent(messageName, TikTokLiveMapperModel::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -66,7 +58,6 @@ public class TikTokLiveMapper implements TikTokMapper {
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public TikTokMapperModel forMessage(Class<? extends GeneratedMessageV3> mapperName, MappingAction<MappingResult> onMapping) {
|
||||
var model = forMessage(mapperName);
|
||||
@@ -84,7 +75,6 @@ public class TikTokLiveMapper implements TikTokMapper {
|
||||
return globalMapperModel;
|
||||
}
|
||||
|
||||
|
||||
public boolean isRegistered(String mapperName) {
|
||||
return mappers.containsKey(mapperName);
|
||||
}
|
||||
@@ -94,23 +84,19 @@ public class TikTokLiveMapper implements TikTokMapper {
|
||||
}
|
||||
|
||||
public List<TikTokEvent> handleMapping(String messageName, byte[] bytes) {
|
||||
if (!isRegistered(messageName)) {
|
||||
return List.of();
|
||||
}
|
||||
var mapperModel = mappers.get(messageName);
|
||||
if (mapperModel == null)
|
||||
return List.of();
|
||||
|
||||
var inputBytes = mapperModel.getOnBeforeMapping().onMapping(bytes, messageName, mapperUtils);
|
||||
var globalInputBytes = globalMapperModel.getOnBeforeMapping().onMapping(inputBytes, messageName, mapperUtils);
|
||||
|
||||
|
||||
var mappingResult = mapperModel.getOnMapping().onMapping(globalInputBytes, messageName, mapperUtils);
|
||||
|
||||
if (mappingResult == null) {
|
||||
if (mappingResult == null)
|
||||
mappingResult = globalMapperModel.getOnMapping().onMapping(globalInputBytes, messageName, mapperUtils);
|
||||
}
|
||||
|
||||
var afterMappingResult = mapperModel.getOnAfterMapping().apply(mappingResult);
|
||||
var globalAfterMappingResult = globalMapperModel.getOnAfterMapping().apply(MappingResult.of(mappingResult.getSource(), afterMappingResult));
|
||||
return globalAfterMappingResult;
|
||||
return globalMapperModel.getOnAfterMapping().apply(MappingResult.of(mappingResult.getSource(), afterMappingResult));
|
||||
}
|
||||
}
|
||||
@@ -24,8 +24,6 @@ package io.github.jwdeveloper.tiktok.websocket;
|
||||
|
||||
import org.java_websocket.WebSocket;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class TikTokWebSocketPingingTask
|
||||
{
|
||||
private Thread thread;
|
||||
@@ -36,8 +34,7 @@ public class TikTokWebSocketPingingTask
|
||||
public void run(WebSocket webSocket, long pingTaskTime)
|
||||
{
|
||||
stop();
|
||||
thread = new Thread(() -> pingTask(webSocket, pingTaskTime));
|
||||
thread.setName("pinging-task");
|
||||
thread = new Thread(() -> pingTask(webSocket, pingTaskTime), "pinging-task");
|
||||
isRunning = true;
|
||||
thread.start();
|
||||
}
|
||||
@@ -51,16 +48,13 @@ public class TikTokWebSocketPingingTask
|
||||
|
||||
private void pingTask(WebSocket webSocket, long pingTaskTime)
|
||||
{
|
||||
var random = new Random();
|
||||
while (isRunning) {
|
||||
try {
|
||||
if (!webSocket.isOpen()) {
|
||||
Thread.sleep(SLEEP_TIME);
|
||||
continue;
|
||||
}
|
||||
if (webSocket.isOpen()) {
|
||||
webSocket.sendPing();
|
||||
|
||||
Thread.sleep(pingTaskTime+random.nextInt(MAX_TIMEOUT));
|
||||
Thread.sleep(pingTaskTime+(int)(Math.random() * MAX_TIMEOUT));
|
||||
} else
|
||||
Thread.sleep(SLEEP_TIME);
|
||||
}
|
||||
catch (Exception e) {
|
||||
//TODO we should display some kind of error message
|
||||
|
||||
Reference in New Issue
Block a user