mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
Update collaboration.md
This commit is contained in:
108
collaboration.md
108
collaboration.md
@@ -92,3 +92,111 @@ Are you willing to help or improve TikTokLiveJava?
|
|||||||
|
|
||||||
#### Tools-ReadmeGenerator
|
#### Tools-ReadmeGenerator
|
||||||
Generates readme file from template
|
Generates readme file from template
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### How to add new Event?
|
||||||
|
|
||||||
|
First step is to create class that represends event. Remember, all the events classes must be located in the `io.github.jwdeveloper.tiktok.data.events` package
|
||||||
|
|
||||||
|
```java
|
||||||
|
package io.github.jwdeveloper.tiktok.data.events;
|
||||||
|
|
||||||
|
import io.github.jwdeveloper.tiktok.annotations.EventMeta;
|
||||||
|
import io.github.jwdeveloper.tiktok.annotations.EventType;
|
||||||
|
import io.github.jwdeveloper.tiktok.data.events.common.TikTokHeaderEvent;
|
||||||
|
import io.github.jwdeveloper.tiktok.messages.data.User;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
|
||||||
|
@Data //lombok annotation
|
||||||
|
@EventMeta(eventType = EventType.Message) //this annotation is used by readme generater code
|
||||||
|
public class CustomEvent extends TikTokHeaderEvent
|
||||||
|
{
|
||||||
|
private final User user;
|
||||||
|
private final String title;
|
||||||
|
|
||||||
|
public CustomEvent(User user,String title)
|
||||||
|
{
|
||||||
|
this.user = user;
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Now we can jump to the `io.github.jwdeveloper.tiktok.handlers.TikTokMessageHandlerRegistration` class. It is used
|
||||||
|
to define mappings from incoming protocolbuffer data to Events.
|
||||||
|
Note that all classes that starts with `Webcast` represents protocolbuffer data that is coming from tiktok
|
||||||
|
Note all `Webcast` classes are generated from `proto` file that is defined in `API/src/main/proto/webcast.proto` I recommand to use `protocolbuffer` plugin for inteliji
|
||||||
|
|
||||||
|
|
||||||
|
For this example we registered new mapping that is triggered every time `WebcastGiftMessage` is comming
|
||||||
|
from TikTok.
|
||||||
|
|
||||||
|
```java
|
||||||
|
public void init() {
|
||||||
|
|
||||||
|
registerMapping(WebcastGiftMessage.class, bytes ->
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
WebcastGiftMessage tiktokData = WebcastGiftMessage.parseFrom(bytes);
|
||||||
|
|
||||||
|
io.github.jwdeveloper.tiktok.messages.data.User tiktokProtocolBufferUser = tiktokData.getUser();
|
||||||
|
io.github.jwdeveloper.tiktok.data.models.users.User tiktokLiveJavaUser = User.map(tiktokProtocolBufferUser);
|
||||||
|
|
||||||
|
return new CustomEvent(tiktokLiveJavaUser, "hello word");
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new TikTokLiveException("Unable to parse our custom event", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//ConnectionEvents events
|
||||||
|
registerMapping(WebcastControlMessage.class, this::handleWebcastControlMessage);
|
||||||
|
|
||||||
|
//Room status events
|
||||||
|
registerMapping(WebcastLiveIntroMessage.class, roomInfoHandler::handleIntro);
|
||||||
|
registerMapping(WebcastRoomUserSeqMessage.class, roomInfoHandler::handleUserRanking);
|
||||||
|
|
||||||
|
registerMapping(WebcastCaptionMessage.class, TikTokCaptionEvent.class);
|
||||||
|
//... more mappings down there
|
||||||
|
}
|
||||||
|
```
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Next step is to open `TikTokLiveClientBuilder` and method for handling our new event
|
||||||
|
|
||||||
|
``` java
|
||||||
|
|
||||||
|
public LiveClientBuilder onCustomEvent(EventConsumer<CustomEvent> event) {
|
||||||
|
tikTokEventHandler.subscribe(CustomEvent.class, event);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
To make this method visible from `TikTokLive.newClient("asds").onCustomEvent()` we
|
||||||
|
need to also add it to interface `EventsBuilder`
|
||||||
|
|
||||||
|
``` java
|
||||||
|
T onCustomEvent(EventConsumer<CustomEvent> event);
|
||||||
|
```
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
Finally we are good to go, out event has been included!
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user