-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
433 additions
and
2,340 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/cool/scx/live_room_watcher/AccessTokenManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cool.scx.live_room_watcher; | ||
|
||
import cool.scx.live_room_watcher.util.Helper; | ||
|
||
import java.io.IOException; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
||
/** | ||
* 官方的被动接受的接口 | ||
*/ | ||
public abstract class AccessTokenManager { | ||
|
||
protected String accessToken; | ||
|
||
protected abstract AccessToken getAccessToken0() throws IOException, InterruptedException; | ||
|
||
/** | ||
* 获取 accessToken | ||
* | ||
* @return a | ||
*/ | ||
public synchronized String getAccessToken() { | ||
if (this.accessToken == null) { | ||
refreshAccessToken(); | ||
} | ||
return this.accessToken; | ||
} | ||
|
||
/** | ||
* 刷新 accessToken | ||
* 首次调用后 会一直循环进行获取 所以理论上讲只需要获取一次 | ||
*/ | ||
public synchronized void refreshAccessToken() { | ||
try { | ||
var accessToken0 = getAccessToken0(); | ||
this.accessToken = accessToken0.accessToken(); | ||
Helper.SCHEDULER.schedule(this::refreshAccessToken, accessToken0.expiresIn() / 2, SECONDS); | ||
} catch (IllegalArgumentException e) { | ||
e.printStackTrace(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
//发生错误的话 2秒后重试 | ||
Helper.SCHEDULER.schedule(this::refreshAccessToken, 2000, SECONDS); | ||
} | ||
} | ||
|
||
} |
146 changes: 36 additions & 110 deletions
146
src/main/java/cool/scx/live_room_watcher/BaseLiveRoomWatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,152 +1,78 @@ | ||
package cool.scx.live_room_watcher; | ||
|
||
import cool.scx.live_room_watcher.message.*; | ||
import io.vertx.core.Vertx; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* <p>Abstract LiveRoomWatcher class.</p> | ||
* | ||
* @author scx567888 | ||
* @version 0.0.1 | ||
* AbstractLiveRoomWatcher | ||
*/ | ||
public abstract class BaseLiveRoomWatcher implements LiveRoomWatcher { | ||
|
||
/** | ||
* Constant <code>vertx</code> | ||
*/ | ||
public static final Vertx vertx; | ||
protected Consumer<Chat> onChat; | ||
protected Consumer<Like> onLike; | ||
protected Consumer<Gift> onGift; | ||
protected Consumer<Follow> onFollow; | ||
protected Consumer<User> onUser; | ||
|
||
/** | ||
* 默认情况下我们什么都不做 | ||
*/ | ||
public static Consumer<Chat> DEFAULT_CHAT_HANDLER = chat -> {}; | ||
|
||
/** | ||
* Constant <code>DEFAULT_USER_HANDLER</code> | ||
*/ | ||
public static Consumer<User> DEFAULT_USER_HANDLER = user -> {}; | ||
|
||
/** | ||
* Constant <code>DEFAULT_LIKE_HANDLER</code> | ||
*/ | ||
public static Consumer<Like> DEFAULT_LIKE_HANDLER = like -> {}; | ||
|
||
/** | ||
* Constant <code>DEFAULT_FOLLOW_HANDLER</code> | ||
*/ | ||
public static Consumer<Follow> DEFAULT_FOLLOW_HANDLER = follow -> {}; | ||
|
||
/** | ||
* Constant <code>DEFAULT_GIFT_HANDLER</code> | ||
*/ | ||
public static Consumer<Gift> DEFAULT_GIFT_HANDLER = gift -> {}; | ||
|
||
static { | ||
vertx = Vertx.vertx(); | ||
} | ||
|
||
protected Consumer<Chat> chatHandler; | ||
protected Consumer<User> userHandler; | ||
protected Consumer<Like> likeHandler; | ||
protected Consumer<Follow> followHandler; | ||
protected Consumer<Gift> giftHandler; | ||
|
||
/** | ||
* <p>Constructor for AbstractLiveRoomWatcher.</p> | ||
*/ | ||
protected BaseLiveRoomWatcher() { | ||
chatHandler = DEFAULT_CHAT_HANDLER; | ||
userHandler = DEFAULT_USER_HANDLER; | ||
likeHandler = DEFAULT_LIKE_HANDLER; | ||
followHandler = DEFAULT_FOLLOW_HANDLER; | ||
giftHandler = DEFAULT_GIFT_HANDLER; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public LiveRoomWatcher chatHandler(Consumer<Chat> handler) { | ||
Objects.requireNonNull(handler); | ||
this.chatHandler = handler; | ||
public LiveRoomWatcher onChat(Consumer<Chat> onChat) { | ||
this.onChat = onChat; | ||
return this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public LiveRoomWatcher userHandler(Consumer<User> handler) { | ||
Objects.requireNonNull(handler); | ||
this.userHandler = handler; | ||
public LiveRoomWatcher onLike(Consumer<Like> onLike) { | ||
this.onLike = onLike; | ||
return this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public LiveRoomWatcher likeHandler(Consumer<Like> handler) { | ||
Objects.requireNonNull(handler); | ||
this.likeHandler = handler; | ||
public LiveRoomWatcher onGift(Consumer<Gift> onGift) { | ||
this.onGift = onGift; | ||
return this; | ||
} | ||
|
||
@Override | ||
public LiveRoomWatcher followHandler(Consumer<Follow> handler) { | ||
Objects.requireNonNull(handler); | ||
this.followHandler = handler; | ||
public LiveRoomWatcher onFollow(Consumer<Follow> onFollow) { | ||
this.onFollow = onFollow; | ||
return this; | ||
} | ||
|
||
@Override | ||
public LiveRoomWatcher giftHandler(Consumer<Gift> handler) { | ||
Objects.requireNonNull(handler); | ||
this.giftHandler = handler; | ||
public LiveRoomWatcher onUser(Consumer<User> onUser) { | ||
this.onUser = onUser; | ||
return this; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Consumer<Chat> chatHandler() { | ||
return chatHandler; | ||
protected void _callOnChat(Chat chat) { | ||
if (this.onChat != null) { | ||
this.onChat.accept(chat); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Consumer<User> userHandler() { | ||
return userHandler; | ||
protected void _callOnLike(Like like) { | ||
if (this.onLike != null) { | ||
this.onLike.accept(like); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Consumer<Like> likeHandler() { | ||
return likeHandler; | ||
protected void _callOnGift(Gift gift) { | ||
if (this.onGift != null) { | ||
this.onGift.accept(gift); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Consumer<Follow> followHandler() { | ||
return followHandler; | ||
protected void _callOnFollow(Follow follow) { | ||
if (this.onFollow != null) { | ||
this.onFollow.accept(follow); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Consumer<Gift> giftHandler() { | ||
return giftHandler; | ||
protected void _callOnUser(User user) { | ||
if (this.onUser != null) { | ||
this.onUser.accept(user); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.