mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-28 01:09:40 -05:00
Events handling
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.messages.WebcastWebsocketMessage;
|
||||
import org.junit.Test;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SerializeWebMessageTest
|
||||
{
|
||||
@Test
|
||||
public void WebcastWebsocketMessage()
|
||||
{
|
||||
try (var str = getClass().getClassLoader().getResourceAsStream("WebcastWebsocketMessage.bin"))
|
||||
{
|
||||
var bytes = str.readAllBytes();
|
||||
var person = WebcastWebsocketMessage.parseFrom(bytes);
|
||||
System.out.println("id: " + person.getId());
|
||||
System.out.println("type: " + person.getType());
|
||||
System.out.println("binary: " + person.getBinary().size());
|
||||
// System.out.println("Email: " + person.getEmail());
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
System.out.println("Error parsing the protobuf message: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,84 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.events.messages.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TikTokLiveTest
|
||||
{
|
||||
public static String TEST_USER_SUBJECT = "live_interactive_";
|
||||
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).build();
|
||||
client.run();
|
||||
|
||||
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user