mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
Test application
This commit is contained in:
@@ -7,6 +7,8 @@ import java.util.function.Consumer;
|
||||
|
||||
|
||||
public interface TikTokEventBuilder<T> {
|
||||
T onUnhandledSocial(Consumer<TikTokUnhandledSocialEvent> event);
|
||||
|
||||
T onLinkMicFanTicket(Consumer<TikTokLinkMicFanTicketEvent> event);
|
||||
|
||||
T onEnvelope(Consumer<TikTokEnvelopeEvent> event);
|
||||
@@ -17,6 +19,8 @@ public interface TikTokEventBuilder<T> {
|
||||
|
||||
T onLinkLayerMessage(Consumer<TikTokLinkLayerMessageEvent> event);
|
||||
|
||||
T onConnected(Consumer<TikTokConnectedEvent> event);
|
||||
|
||||
T onCaption(Consumer<TikTokCaptionEvent> event);
|
||||
|
||||
T onQuestion(Consumer<TikTokQuestionEvent> event);
|
||||
@@ -25,6 +29,8 @@ public interface TikTokEventBuilder<T> {
|
||||
|
||||
T onRoomMessage(Consumer<TikTokRoomMessageEvent> event);
|
||||
|
||||
T onLivePaused(Consumer<TikTokLivePausedEvent> event);
|
||||
|
||||
T onLike(Consumer<TikTokLikeEvent> event);
|
||||
|
||||
T onLinkMessage(Consumer<TikTokLinkMessageEvent> event);
|
||||
@@ -59,17 +65,31 @@ public interface TikTokEventBuilder<T> {
|
||||
|
||||
T onIMDelete(Consumer<TikTokIMDeleteEvent> event);
|
||||
|
||||
T onLiveEnded(Consumer<TikTokLiveEndedEvent> event);
|
||||
|
||||
T onError(Consumer<TikTokErrorEvent> event);
|
||||
|
||||
T onUnhandled(Consumer<TikTokUnhandledEvent> event);
|
||||
|
||||
T onJoin(Consumer<TikTokJoinEvent> event);
|
||||
|
||||
T onRankText(Consumer<TikTokRankTextEvent> event);
|
||||
|
||||
T onShare(Consumer<TikTokShareEvent> event);
|
||||
|
||||
T onUnhandledMember(Consumer<TikTokUnhandledMemberEvent> event);
|
||||
|
||||
T onSubNotify(Consumer<TikTokSubNotifyEvent> event);
|
||||
|
||||
T onLinkMicBattle(Consumer<TikTokLinkMicBattleEvent> event);
|
||||
|
||||
T onDisconnected(Consumer<TikTokDisconnectedEvent> event);
|
||||
|
||||
T onGiftBroadcast(Consumer<TikTokGiftBroadcastEvent> event);
|
||||
|
||||
T onUnhandledControl(Consumer<TikTokUnhandledControlEvent> event);
|
||||
|
||||
T onEvent(Consumer<TikTokEvent> event);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package io.github.jwdeveloper.tiktok.events.messages;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.events.TikTokEvent;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class TikTokErrorEvent extends TikTokEvent
|
||||
{
|
||||
private final Throwable exception;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package io.github.jwdeveloper.tiktok.exceptions;
|
||||
|
||||
public class TikTokLiveMessageParsingException extends TikTokLiveException
|
||||
{
|
||||
public TikTokLiveMessageParsingException() {
|
||||
}
|
||||
|
||||
public TikTokLiveMessageParsingException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public TikTokLiveMessageParsingException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public TikTokLiveMessageParsingException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public TikTokLiveMessageParsingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
||||
@@ -82,8 +82,9 @@ public class TikTokLiveClient implements LiveClient {
|
||||
setState(ConnectionState.CONNECTED);
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
|
||||
public void disconnect()
|
||||
{
|
||||
webSocketClient.stop();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -102,7 +102,13 @@ public class TikTokLiveClientBuilder implements TikTokEventBuilder<TikTokLiveCli
|
||||
var apiService = new TikTokApiService(apiClient, logger, clientParameters);
|
||||
var giftManager = new TikTokGiftManager(logger, apiService, clientSettings);
|
||||
var webResponseHandler = new WebResponseHandler(tikTokEventHandler,giftManager);
|
||||
var webSocketClient = new TikTokWebsocketClient(logger, cookieJar, clientParameters, requestFactory, clientSettings, webResponseHandler);
|
||||
var webSocketClient = new TikTokWebsocketClient(logger,
|
||||
cookieJar,
|
||||
clientParameters,
|
||||
requestFactory,
|
||||
clientSettings,
|
||||
webResponseHandler,
|
||||
tikTokEventHandler);
|
||||
|
||||
return new TikTokLiveClient(meta, apiService, webSocketClient, giftManager, tikTokEventHandler, logger);
|
||||
}
|
||||
@@ -113,6 +119,11 @@ public class TikTokLiveClientBuilder implements TikTokEventBuilder<TikTokLiveCli
|
||||
return client;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onUnhandledSocial(Consumer<TikTokUnhandledSocialEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokUnhandledSocialEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onLinkMicFanTicket(Consumer<TikTokLinkMicFanTicketEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokLinkMicFanTicketEvent.class,event);
|
||||
return this;
|
||||
@@ -138,11 +149,6 @@ public class TikTokLiveClientBuilder implements TikTokEventBuilder<TikTokLiveCli
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onDisconnected(Consumer<TikTokDisconnectedEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokDisconnectedEvent.class, event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onConnected(Consumer<TikTokConnectedEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokConnectedEvent.class,event);
|
||||
return this;
|
||||
@@ -168,6 +174,11 @@ public class TikTokLiveClientBuilder implements TikTokEventBuilder<TikTokLiveCli
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onLivePaused(Consumer<TikTokLivePausedEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokLivePausedEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onLike(Consumer<TikTokLikeEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokLikeEvent.class,event);
|
||||
return this;
|
||||
@@ -198,7 +209,8 @@ public class TikTokLiveClientBuilder implements TikTokEventBuilder<TikTokLiveCli
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onUnauthorizedMember(Consumer<TikTokUnauthorizedMemberEvent> event) {
|
||||
public TikTokLiveClientBuilder onUnauthorizedMember(
|
||||
Consumer<TikTokUnauthorizedMemberEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokUnauthorizedMemberEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
@@ -253,6 +265,21 @@ public class TikTokLiveClientBuilder implements TikTokEventBuilder<TikTokLiveCli
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onLiveEnded(Consumer<TikTokLiveEndedEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokLiveEndedEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onError(Consumer<TikTokErrorEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokErrorEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onUnhandled(Consumer<TikTokUnhandledEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokUnhandledEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onJoin(Consumer<TikTokJoinEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokJoinEvent.class,event);
|
||||
return this;
|
||||
@@ -268,6 +295,11 @@ public class TikTokLiveClientBuilder implements TikTokEventBuilder<TikTokLiveCli
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onUnhandledMember(Consumer<TikTokUnhandledMemberEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokUnhandledMemberEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onSubNotify(Consumer<TikTokSubNotifyEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokSubNotifyEvent.class,event);
|
||||
return this;
|
||||
@@ -278,13 +310,22 @@ public class TikTokLiveClientBuilder implements TikTokEventBuilder<TikTokLiveCli
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onDisconnected(Consumer<TikTokDisconnectedEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokDisconnectedEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onGiftBroadcast(Consumer<TikTokGiftBroadcastEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokGiftBroadcastEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onEvent(Consumer<TikTokEvent> event)
|
||||
{
|
||||
public TikTokLiveClientBuilder onUnhandledControl(Consumer<TikTokUnhandledControlEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokUnhandledControlEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TikTokLiveClientBuilder onEvent(Consumer<TikTokEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokEvent.class,event);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@ package io.github.jwdeveloper.tiktok.handlers;
|
||||
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.github.jwdeveloper.tiktok.events.messages.*;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.events.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.messages.*;
|
||||
import io.github.jwdeveloper.tiktok.events.messages.TikTokErrorEvent;
|
||||
import io.github.jwdeveloper.tiktok.events.messages.TikTokUnhandledEvent;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveMessageParsingException;
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastResponse;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
@@ -18,20 +21,18 @@ public abstract class WebResponseHandlerBase {
|
||||
private final Map<String, TikTokMessageHandler> handlers;
|
||||
private final TikTokEventHandler tikTokEventHandler;
|
||||
|
||||
public WebResponseHandlerBase(TikTokEventHandler tikTokEventHandler)
|
||||
{
|
||||
public WebResponseHandlerBase(TikTokEventHandler tikTokEventHandler) {
|
||||
handlers = new HashMap<>();
|
||||
this.tikTokEventHandler = tikTokEventHandler;
|
||||
init();
|
||||
}
|
||||
|
||||
public abstract void init();
|
||||
|
||||
public void register(Class<?> input, Class<?> output)
|
||||
{
|
||||
public void register(Class<?> input, Class<?> output) {
|
||||
register(input, (e) ->
|
||||
{
|
||||
try
|
||||
{
|
||||
try {
|
||||
var parseMethod = input.getDeclaredMethod("parseFrom", ByteString.class);
|
||||
var deserialized = parseMethod.invoke(null, e.getBinary());
|
||||
|
||||
@@ -39,15 +40,14 @@ public abstract class WebResponseHandlerBase {
|
||||
|
||||
var tiktokEvent = constructors.get().newInstance(deserialized);
|
||||
return (TikTokEvent)tiktokEvent;
|
||||
}
|
||||
catch (Exception ex)
|
||||
} catch (Exception ex)
|
||||
{
|
||||
throw new TikTokLiveException("Unable to handle parsing from class: "+input.getSimpleName()+" to class "+output.getSimpleName(),ex);
|
||||
throw new TikTokLiveMessageParsingException("Unable to handle parsing from class: " + input.getSimpleName() + " to class " + output.getSimpleName(), ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
public <T> void register(Class clazz, Function<WebcastResponse.Message,TikTokEvent> func)
|
||||
{
|
||||
|
||||
public <T> void register(Class clazz, Function<WebcastResponse.Message, TikTokEvent> func) {
|
||||
var haandler = new TikTokMessageHandler<T>() {
|
||||
@Override
|
||||
public Class<T> getHandleClazz() {
|
||||
@@ -64,21 +64,21 @@ public abstract class WebResponseHandlerBase {
|
||||
}
|
||||
|
||||
public void handle(WebcastResponse webcastResponse) {
|
||||
// System.out.println("==============================================================");
|
||||
// System.out.println("Getting messages: " + webcastResponse.getMessagesList().size());
|
||||
for (var message : webcastResponse.getMessagesList()) {
|
||||
try {
|
||||
handleSingleMessage(message);
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Error whilst Handling Message. Stopping Client.{Environment.NewLine}Final Message: {Convert.ToBase64String(message.Binary)}", e);
|
||||
} catch (Exception e)
|
||||
{
|
||||
var decoded = Base64.getEncoder().encodeToString(message.getBinary().toByteArray());
|
||||
|
||||
var exception = new TikTokLiveException("Error whilst Handling Message. Stopping Client. Final Message: \n"+decoded, e);
|
||||
tikTokEventHandler.publish(new TikTokErrorEvent(exception));
|
||||
}
|
||||
}
|
||||
// System.out.println("==============================================================");
|
||||
}
|
||||
|
||||
private void handleSingleMessage(WebcastResponse.Message message) throws Exception {
|
||||
if(!handlers.containsKey(message.getType()))
|
||||
{
|
||||
if (!handlers.containsKey(message.getType())) {
|
||||
tikTokEventHandler.publish(new TikTokUnhandledEvent(message));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package io.github.jwdeveloper.tiktok.websocket;
|
||||
|
||||
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.events.messages.TikTokConnectedEvent;
|
||||
import io.github.jwdeveloper.tiktok.events.messages.TikTokDisconnectedEvent;
|
||||
import io.github.jwdeveloper.tiktok.events.messages.TikTokErrorEvent;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveMessageParsingException;
|
||||
import io.github.jwdeveloper.tiktok.handlers.TikTokEventHandler;
|
||||
import io.github.jwdeveloper.tiktok.handlers.WebResponseHandler;
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastResponse;
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastWebsocketAck;
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastWebsocketMessage;
|
||||
@@ -9,35 +14,55 @@ import io.github.jwdeveloper.tiktok.messages.WebcastWebsocketMessage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.net.http.WebSocket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
|
||||
public class TikTokWebSocketListener implements java.net.http.WebSocket.Listener {
|
||||
|
||||
private ByteArrayOutputStream accumulatedData = new ByteArrayOutputStream();
|
||||
private final ByteArrayOutputStream accumulatedData = new ByteArrayOutputStream();
|
||||
private final WebResponseHandler webResponseHandler;
|
||||
private final TikTokEventHandler tikTokEventHandler;
|
||||
|
||||
public TikTokWebSocketListener(WebResponseHandler webResponseHandler, TikTokEventHandler tikTokEventHandler) {
|
||||
this.webResponseHandler = webResponseHandler;
|
||||
this.tikTokEventHandler = tikTokEventHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<?> onBinary(WebSocket webSocket, ByteBuffer data, boolean last) {
|
||||
try {
|
||||
var decoded = Base64.getEncoder().encodeToString(data.array());
|
||||
System.out.println(decoded);
|
||||
var bytes = new byte[data.remaining()];
|
||||
data.get(bytes);
|
||||
accumulatedData.write(bytes);
|
||||
if (last) {
|
||||
|
||||
//handleBinary(webSocket, accumulatedData.toByteArray());
|
||||
handleBinary(webSocket, accumulatedData.toByteArray());
|
||||
accumulatedData.reset();
|
||||
accumulatedData = new ByteArrayOutputStream();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
tikTokEventHandler.publish(new TikTokErrorEvent(e));
|
||||
}
|
||||
webSocket.request(1);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(java.net.http.WebSocket webSocket) {
|
||||
tikTokEventHandler.publish(new TikTokConnectedEvent());
|
||||
webSocket.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(java.net.http.WebSocket webSocket, Throwable error) {
|
||||
tikTokEventHandler.publish(new TikTokErrorEvent(error));
|
||||
webSocket.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<?> onClose(java.net.http.WebSocket webSocket, int statusCode, String reason) {
|
||||
tikTokEventHandler.publish(new TikTokDisconnectedEvent());
|
||||
return java.net.http.WebSocket.Listener.super.onClose(webSocket, statusCode, reason);
|
||||
}
|
||||
|
||||
private void handleBinary(WebSocket webSocket, byte[] buffer) {
|
||||
try {
|
||||
|
||||
@@ -46,22 +71,28 @@ public class TikTokWebSocketListener implements java.net.http.WebSocket.Listener
|
||||
return;
|
||||
}
|
||||
sendAckId(webSocket, websocketMessage.getId());
|
||||
|
||||
|
||||
try {
|
||||
|
||||
//error here
|
||||
var response = WebcastResponse.parseFrom(websocketMessage.getBinary());
|
||||
System.out.println("Works");
|
||||
// handleResponse(response);
|
||||
webResponseHandler.handle(response);
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Unabel to read WebcastResponse", e);
|
||||
throw new TikTokLiveMessageParsingException("Unable to read WebcastResponse", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Unabel to read WebcastWebsocketMessage", e);
|
||||
throw new TikTokLiveMessageParsingException("Unable to read WebcastWebsocketMessage", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void pingTask(WebSocket webSocket) throws InterruptedException {
|
||||
while (true) {
|
||||
byte[] message = new byte[]{58, 2, 104, 98};
|
||||
ByteBuffer buffer = ByteBuffer.wrap(message);
|
||||
while (buffer.hasRemaining()) {
|
||||
webSocket.sendPing(buffer);
|
||||
}
|
||||
buffer.clear();
|
||||
Thread.sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendAckId(WebSocket webSocket, long id) {
|
||||
var serverInfo = WebcastWebsocketAck
|
||||
@@ -72,29 +103,4 @@ public class TikTokWebSocketListener implements java.net.http.WebSocket.Listener
|
||||
webSocket.sendBinary(serverInfo.toByteString().asReadOnlyByteBuffer(), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(java.net.http.WebSocket webSocket) {
|
||||
System.out.println("WebSocket opened");
|
||||
webSocket.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(java.net.http.WebSocket webSocket, Throwable error) {
|
||||
System.out.println("Error occurred: " + error.getMessage());
|
||||
webSocket.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<?> onText(java.net.http.WebSocket webSocket, CharSequence data, boolean last) {
|
||||
System.out.println("Received onText: " + data);
|
||||
return java.net.http.WebSocket.Listener.super.onText(webSocket, data, last);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<?> onClose(java.net.http.WebSocket webSocket, int statusCode, String reason) {
|
||||
System.out.println("WebSocket closed with status code: " + statusCode + " and reason: " + reason);
|
||||
return java.net.http.WebSocket.Listener.super.onClose(webSocket, statusCode, reason);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -4,14 +4,15 @@ package io.github.jwdeveloper.tiktok.websocket;
|
||||
import io.github.jwdeveloper.tiktok.ClientSettings;
|
||||
import io.github.jwdeveloper.tiktok.Constants;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.handlers.WebResponseHandlerBase;
|
||||
import io.github.jwdeveloper.tiktok.handlers.TikTokEventHandler;
|
||||
import io.github.jwdeveloper.tiktok.handlers.WebResponseHandler;
|
||||
import io.github.jwdeveloper.tiktok.http.HttpUtils;
|
||||
import io.github.jwdeveloper.tiktok.http.TikTokCookieJar;
|
||||
import io.github.jwdeveloper.tiktok.http.TikTokHttpRequestFactory;
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastResponse;
|
||||
import org.java_websocket.drafts.Draft_6455;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.WebSocket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
@@ -25,119 +26,85 @@ public class TikTokWebsocketClient {
|
||||
private final ClientSettings clientSettings;
|
||||
private final TikTokCookieJar tikTokCookieJar;
|
||||
private final TikTokHttpRequestFactory factory;
|
||||
private final WebResponseHandlerBase webResponseHandler;
|
||||
private final WebResponseHandler webResponseHandler;
|
||||
private final TikTokEventHandler tikTokEventHandler;
|
||||
|
||||
private WebSocket webSocket;
|
||||
|
||||
private boolean isConnected;
|
||||
|
||||
public TikTokWebsocketClient(Logger logger,
|
||||
TikTokCookieJar tikTokCookieJar,
|
||||
Map<String, Object> clientParams,
|
||||
TikTokHttpRequestFactory factory,
|
||||
ClientSettings clientSettings,
|
||||
WebResponseHandlerBase webResponseHandler) {
|
||||
WebResponseHandler webResponseHandler,
|
||||
TikTokEventHandler tikTokEventHandler) {
|
||||
this.logger = logger;
|
||||
this.clientParams = clientParams;
|
||||
this.tikTokCookieJar = tikTokCookieJar;
|
||||
this.clientSettings = clientSettings;
|
||||
this.factory = factory;
|
||||
this.webResponseHandler = webResponseHandler;
|
||||
this.tikTokEventHandler = tikTokEventHandler;
|
||||
isConnected = false;
|
||||
}
|
||||
|
||||
public void start(WebcastResponse webcastResponse) {
|
||||
public void start(WebcastResponse webcastResponse)
|
||||
{
|
||||
if(isConnected)
|
||||
{
|
||||
stop();
|
||||
}
|
||||
if (webcastResponse.getSocketUrl().isEmpty() || webcastResponse.getSocketParamsList().isEmpty()) {
|
||||
throw new TikTokLiveException("Could not find Room");
|
||||
}
|
||||
try {
|
||||
var url =getWebSocketUrl(webcastResponse);
|
||||
startWebSocket(url);
|
||||
if (clientSettings.isHandleExistingMessagesOnConnect()) {
|
||||
// HandleWebcastMessages(webcastResponse);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Failed to connect to the websocket", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String getWebSocketUrl(WebcastResponse webcastResponse) {
|
||||
var params = webcastResponse.getSocketParamsList().get(0);
|
||||
var name = params.getName();
|
||||
var value = params.getValue();
|
||||
// System.out.println("KEY: " + name + " value: " + value);
|
||||
|
||||
|
||||
var headers = Constants.DefaultRequestHeaders();
|
||||
|
||||
|
||||
var clone = new TreeMap<>(clientParams);
|
||||
clone.putAll(headers);
|
||||
clone.put(name, value);
|
||||
//clone.put("compress", "gzip");
|
||||
var url = webcastResponse.getSocketUrl();
|
||||
var wsUrl = HttpUtils.parseParametersEncode(url, clone);
|
||||
logger.info("Starting Socket-Threads");
|
||||
//runningTask = Task.Run(WebSocketLoop, token);
|
||||
//pollingTask = Task.Run(PingLoop, token);
|
||||
startWS2(wsUrl);
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Failed to connect to the websocket", e);
|
||||
}
|
||||
if (clientSettings.isHandleExistingMessagesOnConnect()) {
|
||||
try {
|
||||
// HandleWebcastMessages(webcastResponse);
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Error Handling Initial Messages", e);
|
||||
}
|
||||
}
|
||||
return HttpUtils.parseParametersEncode(url, clone);
|
||||
}
|
||||
|
||||
public void startWS(String url) {
|
||||
try {
|
||||
private WebSocket startWebSocket(String url) throws Exception {
|
||||
var cookie = tikTokCookieJar.parseCookies();
|
||||
System.out.println("WssIP: " + url);
|
||||
System.out.println("Cookie: " + cookie);
|
||||
// System.out.println("WssIP: " + url);
|
||||
// System.out.println("Cookie: " + cookie);
|
||||
|
||||
var map = new HashMap<String, String>();
|
||||
map.put("Cookie", cookie);
|
||||
|
||||
var ws = factory.openSocket()
|
||||
return factory.openSocket()
|
||||
.subprotocols("echo-protocol")
|
||||
.connectTimeout(Duration.ofSeconds(15))
|
||||
.header("Cookie", cookie)
|
||||
.buildAsync(URI.create(url), new TikTokWebSocketListener()).get();
|
||||
|
||||
|
||||
while (true) {
|
||||
byte[] message = new byte[]{58, 2, 104, 98};
|
||||
ByteBuffer buffer = ByteBuffer.wrap(message);
|
||||
while (buffer.hasRemaining()) {
|
||||
ws.sendPing(buffer);
|
||||
}
|
||||
buffer.clear();
|
||||
Thread.sleep(10);
|
||||
.buildAsync(URI.create(url), new TikTokWebSocketListener(webResponseHandler, tikTokEventHandler)).get();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void startWS2(String url) {
|
||||
try {
|
||||
var cookie = tikTokCookieJar.parseCookies();
|
||||
System.out.println("WssIP: " + url);
|
||||
System.out.println("Cookie: " + cookie);
|
||||
|
||||
var map = new HashMap<String, String>();
|
||||
map.put("Cookie", cookie);
|
||||
|
||||
var client = new WebSocketClientTest(URI.create(url), new Draft_6455(), map, 1500,webResponseHandler);
|
||||
client.connect();
|
||||
/*
|
||||
while (true) {
|
||||
byte[] message = new byte[]{58, 2, 104, 98};
|
||||
ByteBuffer buffer = ByteBuffer.wrap(message);
|
||||
while (buffer.hasRemaining()) {
|
||||
// client.send(buffer);
|
||||
client.sendPing();
|
||||
}
|
||||
buffer.clear();
|
||||
Thread.sleep(10);
|
||||
}*/
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
|
||||
if(isConnected && webSocket != null)
|
||||
{
|
||||
webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
package io.github.jwdeveloper.tiktok.websocket;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
import io.github.jwdeveloper.tiktok.exceptions.TikTokLiveException;
|
||||
import io.github.jwdeveloper.tiktok.handlers.WebResponseHandlerBase;
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastResponse;
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastWebsocketAck;
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastWebsocketMessage;
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.java_websocket.drafts.Draft;
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
public class WebSocketClientTest extends WebSocketClient {
|
||||
|
||||
private boolean debbug = false;
|
||||
|
||||
private final WebResponseHandlerBase webResponseHandler;
|
||||
|
||||
|
||||
public WebSocketClientTest(URI serverUri,
|
||||
Draft protocolDraft,
|
||||
Map<String, String> httpHeaders,
|
||||
int connectTimeout,
|
||||
WebResponseHandlerBase webResponseHandler) {
|
||||
super(serverUri, protocolDraft, httpHeaders, connectTimeout);
|
||||
this.webResponseHandler = webResponseHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(ByteBuffer data) {
|
||||
sendPing();
|
||||
//System.out.println("onMessage Binary");
|
||||
var bytes = new byte[data.remaining()];
|
||||
data.get(bytes);
|
||||
if(debbug)
|
||||
{
|
||||
var decoded = Base64.getEncoder().encodeToString(bytes);
|
||||
// System.out.println(decoded);
|
||||
}
|
||||
handleBinary(bytes);
|
||||
}
|
||||
|
||||
private void handleBinary(byte[] buffer) {
|
||||
try {
|
||||
|
||||
var websocketMessage = WebcastWebsocketMessage.parseFrom(buffer);
|
||||
if (websocketMessage.getBinary().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
var response = WebcastResponse.parseFrom(websocketMessage.getBinary());
|
||||
sendAckId(websocketMessage.getId());
|
||||
webResponseHandler.handle(response);
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Unabel to read WebcastResponse", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new TikTokLiveException("Unabel to read WebcastWebsocketMessage", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public byte[] unGunzipFile(ByteString byteString) {
|
||||
|
||||
try {
|
||||
|
||||
GZIPInputStream gZIPInputStream = new GZIPInputStream(byteString.newInput());
|
||||
|
||||
|
||||
var bytes = gZIPInputStream.readAllBytes();
|
||||
|
||||
gZIPInputStream.close();
|
||||
|
||||
|
||||
return bytes;
|
||||
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return new byte[0];
|
||||
}
|
||||
private void sendAckId(long id) {
|
||||
var serverInfo = WebcastWebsocketAck
|
||||
.newBuilder()
|
||||
.setType("ack")
|
||||
.setId(id)
|
||||
.build();
|
||||
send(serverInfo.toByteString().asReadOnlyByteBuffer());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onOpen(ServerHandshake serverHandshake) {
|
||||
System.out.println("onOpen");
|
||||
sendPing();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String s) {
|
||||
sendPing();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(int i, String s, boolean b) {
|
||||
System.out.println("onClose");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
System.out.println("error");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
|
||||
import io.github.jwdeveloper.tiktok.events.messages.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TikTokLiveTest {
|
||||
public static String TEST_USER_SUBJECT = "tv_asahi_news";
|
||||
|
||||
|
||||
@Test
|
||||
public void ShouldConnect() throws IOException {
|
||||
var client = TikTokLive.newClient(TEST_USER_SUBJECT)
|
||||
.onConnected(this::onConnected)
|
||||
.onDisconnected(this::onDisconnected)
|
||||
.onRoomViewerData(this::onViewerData)
|
||||
.onJoin(this::onJoin)
|
||||
.onComment(this::onComment)
|
||||
.onFollow(this::onFollow)
|
||||
.onShare(this::onShare)
|
||||
.onSubscribe(this::onSubscribe)
|
||||
.onLike(this::onLike)
|
||||
.onGiftMessage(this::onGiftMessage)
|
||||
.onEmote(this::onEmote)
|
||||
.buildAndRun();
|
||||
System.in.read();
|
||||
|
||||
}
|
||||
private void onConnected(TikTokConnectedEvent e) {
|
||||
print("Connected");
|
||||
}
|
||||
|
||||
private void onDisconnected(TikTokDisconnectedEvent e) {
|
||||
print("Disconnected");
|
||||
}
|
||||
|
||||
private void onViewerData(TikTokRoomViewerDataEvent e) {
|
||||
print("Viewer count is:", e.getViewerCount());
|
||||
}
|
||||
|
||||
private void onJoin(TikTokJoinEvent e) {
|
||||
print(e.getUser().getUniqueId(), "joined!");
|
||||
}
|
||||
|
||||
private void onComment(TikTokCommentEvent e) {
|
||||
print(e.getUser().getUniqueId(), e.getText());
|
||||
}
|
||||
|
||||
private void onFollow(TikTokFollowEvent e) {
|
||||
print(e.getNewFollower().getUniqueId(), "followed!");
|
||||
}
|
||||
|
||||
private void onShare(TikTokShareEvent e) {
|
||||
print(e.getUser().getUniqueId(), "shared!");
|
||||
}
|
||||
|
||||
private void onSubscribe(TikTokSubscribeEvent e) {
|
||||
print(e.getNewSubscriber().getUniqueId(), "subscribed!");
|
||||
}
|
||||
|
||||
private void onLike(TikTokLikeEvent e) {
|
||||
|
||||
print(e.getSender().getUniqueId(), "liked!");
|
||||
}
|
||||
|
||||
private void onGiftMessage(TikTokGiftMessageEvent e) {
|
||||
print(e.getSender().getUniqueId(), "sent", e.getAmount(), "x", e.getGift().getName());
|
||||
}
|
||||
|
||||
private void onEmote(TikTokEmoteEvent e) {
|
||||
print(e.getUser().getUniqueId(), "sent", e.getEmoteId());
|
||||
}
|
||||
|
||||
private static void print(Object... messages) {
|
||||
var sb = new StringBuilder();
|
||||
for (var message : messages) {
|
||||
sb.append(message).append(" ");
|
||||
}
|
||||
System.out.println(sb.toString());
|
||||
}
|
||||
|
||||
}
|
||||
28
TestApplication/pom.xml
Normal file
28
TestApplication/pom.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>TestApplication</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<artifactId>Client</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>16</maven.compiler.source>
|
||||
<maven.compiler.target>16</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,87 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.events.messages.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static String TEST_USER_SUBJECT = "tv_asahi_news";
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
var client = TikTokLive.newClient(TEST_USER_SUBJECT)
|
||||
.onConnected(Main::onConnected)
|
||||
.onDisconnected(Main::onDisconnected)
|
||||
.onRoomViewerData(Main::onViewerData)
|
||||
.onJoin(Main::onJoin)
|
||||
.onComment(Main::onComment)
|
||||
.onFollow(Main::onFollow)
|
||||
.onShare(Main::onShare)
|
||||
.onSubscribe(Main::onSubscribe)
|
||||
.onLike(Main::onLike)
|
||||
.onGiftMessage(Main::onGiftMessage)
|
||||
.onEmote(Main::onEmote)
|
||||
.onError(tikTokErrorEvent ->
|
||||
{
|
||||
tikTokErrorEvent.getException().printStackTrace();
|
||||
})
|
||||
.buildAndRun();
|
||||
|
||||
|
||||
var viewers = client.getMeta().getViewersCount();
|
||||
System.in.read();
|
||||
}
|
||||
|
||||
private static void onConnected(TikTokConnectedEvent e) {
|
||||
print("Connected");
|
||||
}
|
||||
|
||||
private static void onDisconnected(TikTokDisconnectedEvent e) {
|
||||
print("Disconnected");
|
||||
}
|
||||
|
||||
private static void onViewerData(TikTokRoomViewerDataEvent e) {
|
||||
print("Viewer count is:", e.getViewerCount());
|
||||
}
|
||||
|
||||
private static void onJoin(TikTokJoinEvent e) {
|
||||
print(e.getUser().getUniqueId(), "joined!");
|
||||
}
|
||||
|
||||
private static void onComment(TikTokCommentEvent e) {
|
||||
print(e.getUser().getUniqueId(), e.getText());
|
||||
}
|
||||
|
||||
private static void onFollow(TikTokFollowEvent e) {
|
||||
print(e.getNewFollower().getUniqueId(), "followed!");
|
||||
}
|
||||
|
||||
private static void onShare(TikTokShareEvent e) {
|
||||
print(e.getUser().getUniqueId(), "shared!");
|
||||
}
|
||||
|
||||
private static void onSubscribe(TikTokSubscribeEvent e) {
|
||||
print(e.getNewSubscriber().getUniqueId(), "subscribed!");
|
||||
}
|
||||
|
||||
private static void onLike(TikTokLikeEvent e) {
|
||||
|
||||
print(e.getSender().getUniqueId(), "liked!");
|
||||
}
|
||||
|
||||
private static void onGiftMessage(TikTokGiftMessageEvent e) {
|
||||
print(e.getSender().getUniqueId(), "sent", e.getAmount(), "x", e.getGift().getName());
|
||||
}
|
||||
|
||||
private static void onEmote(TikTokEmoteEvent e) {
|
||||
print(e.getUser().getUniqueId(), "sent", e.getEmoteId());
|
||||
}
|
||||
|
||||
private static void print(Object... messages) {
|
||||
var sb = new StringBuilder();
|
||||
for (var message : messages) {
|
||||
sb.append(message).append(" ");
|
||||
}
|
||||
System.out.println(sb.toString());
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
||||
@@ -36,8 +36,13 @@ public class EventsInterfaceGenerator {
|
||||
// Generate constructors
|
||||
for (var clazz : eventsClasses) {
|
||||
var clazzName = clazz.getSimpleName();
|
||||
var methodName = clazzName.replace("TikTok", "");
|
||||
|
||||
var methodName = clazzName;
|
||||
methodName = clazzName.replace("TikTok", "");
|
||||
if(!clazz.equals(TikTokEvent.class))
|
||||
{
|
||||
methodName = methodName.replace("Event", "");
|
||||
}
|
||||
MethodSpec.Builder constructorBuilder = MethodSpec.methodBuilder("on" + methodName);
|
||||
|
||||
|
||||
@@ -76,8 +81,13 @@ public class EventsInterfaceGenerator {
|
||||
// Generate constructors
|
||||
for (var clazz : eventsClasses) {
|
||||
var clazzName = clazz.getSimpleName();
|
||||
var methodName = clazzName.replace("TikTok", "");
|
||||
methodName ="on" + methodName.replace("Event", "");
|
||||
var methodName = clazzName;
|
||||
methodName = clazzName.replace("TikTok", "");
|
||||
if(!clazz.equals(TikTokEvent.class))
|
||||
{
|
||||
methodName = methodName.replace("Event", "");
|
||||
}
|
||||
methodName ="on" + methodName;
|
||||
MethodSpec.Builder constructorBuilder = MethodSpec.methodBuilder( methodName);
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user