-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from RakSrinaNa/feature/ws-chat
Connect to chat using WebSocket
- Loading branch information
Showing
51 changed files
with
1,069 additions
and
229 deletions.
There are no files selected for viewing
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
Binary file not shown.
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,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
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
12 changes: 12 additions & 0 deletions
12
miner/src/main/java/fr/raksrinana/channelpointsminer/miner/api/chat/ITwitchChatClient.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,12 @@ | ||
package fr.raksrinana.channelpointsminer.miner.api.chat; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
public interface ITwitchChatClient extends AutoCloseable{ | ||
void join(@NotNull String channel); | ||
|
||
void leave(@NotNull String channel); | ||
|
||
@Override | ||
void close(); | ||
} |
30 changes: 30 additions & 0 deletions
30
miner/src/main/java/fr/raksrinana/channelpointsminer/miner/api/chat/TwitchChatFactory.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,30 @@ | ||
package fr.raksrinana.channelpointsminer.miner.api.chat; | ||
|
||
import fr.raksrinana.channelpointsminer.miner.api.chat.irc.TwitchIrcChatClient; | ||
import fr.raksrinana.channelpointsminer.miner.api.chat.ws.TwitchChatWebSocketPool; | ||
import fr.raksrinana.channelpointsminer.miner.api.passport.TwitchLogin; | ||
import fr.raksrinana.channelpointsminer.miner.config.ChatMode; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class TwitchChatFactory{ | ||
@NotNull | ||
public static ITwitchChatClient createChat(@NotNull ChatMode chatMode, @NotNull TwitchLogin twitchLogin){ | ||
return switch(chatMode){ | ||
case IRC -> createIrcChat(twitchLogin); | ||
case WS -> createWsChat(twitchLogin); | ||
}; | ||
} | ||
|
||
@NotNull | ||
private static ITwitchChatClient createIrcChat(@NotNull TwitchLogin twitchLogin){ | ||
return new TwitchIrcChatClient(twitchLogin); | ||
} | ||
|
||
@NotNull | ||
private static ITwitchChatClient createWsChat(@NotNull TwitchLogin twitchLogin){ | ||
return new TwitchChatWebSocketPool(Integer.MAX_VALUE, twitchLogin); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...ner/miner/irc/TwitchIrcEventListener.java → .../api/chat/irc/TwitchIrcEventListener.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
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
8 changes: 8 additions & 0 deletions
8
...java/fr/raksrinana/channelpointsminer/miner/api/chat/ws/ITwitchChatWebSocketListener.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,8 @@ | ||
package fr.raksrinana.channelpointsminer.miner.api.chat.ws; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public interface ITwitchChatWebSocketListener{ | ||
void onWebSocketClosed(@NotNull TwitchChatWebSocketClient client, int code, @Nullable String reason, boolean remote); | ||
} |
123 changes: 123 additions & 0 deletions
123
...in/java/fr/raksrinana/channelpointsminer/miner/api/chat/ws/TwitchChatWebSocketClient.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,123 @@ | ||
package fr.raksrinana.channelpointsminer.miner.api.chat.ws; | ||
|
||
import fr.raksrinana.channelpointsminer.miner.api.chat.ITwitchChatClient; | ||
import fr.raksrinana.channelpointsminer.miner.api.passport.TwitchLogin; | ||
import fr.raksrinana.channelpointsminer.miner.factory.TimeFactory; | ||
import fr.raksrinana.channelpointsminer.miner.log.LogContext; | ||
import lombok.Getter; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.java_websocket.WebSocket; | ||
import org.java_websocket.client.WebSocketClient; | ||
import org.java_websocket.framing.Framedata; | ||
import org.java_websocket.handshake.ServerHandshake; | ||
import org.jetbrains.annotations.NotNull; | ||
import java.net.URI; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
@Log4j2 | ||
public class TwitchChatWebSocketClient extends WebSocketClient implements ITwitchChatClient{ | ||
@Getter | ||
private final Set<String> channels; | ||
private final List<ITwitchChatWebSocketListener> listeners; | ||
@Getter | ||
private final String uuid; | ||
private final TwitchLogin twitchLogin; | ||
|
||
@Getter | ||
private Instant lastPing; | ||
|
||
public TwitchChatWebSocketClient(@NotNull URI uri, @NotNull TwitchLogin twitchLogin){ | ||
super(uri); | ||
this.twitchLogin = twitchLogin; | ||
uuid = UUID.randomUUID().toString(); | ||
|
||
setConnectionLostTimeout(0); | ||
channels = new HashSet<>(); | ||
listeners = new ArrayList<>(); | ||
lastPing = Instant.EPOCH; | ||
} | ||
|
||
@Override | ||
public void onOpen(ServerHandshake serverHandshake){ | ||
try(var ignored = LogContext.empty().withSocketId(uuid)){ | ||
log.info("IRC WebSocket opened"); | ||
onPing(); | ||
sendMessage("CAP REQ :twitch.tv/tags twitch.tv/commands"); | ||
send("PASS oauth:%s".formatted(twitchLogin.getAccessToken())); | ||
sendMessage("NICK %s".formatted(twitchLogin.getUsername().toLowerCase())); | ||
} | ||
} | ||
|
||
@Override | ||
public void onMessage(String messageStr){ | ||
try(var logContext = LogContext.empty().withSocketId(uuid)){ | ||
log.trace("Received IRC Websocket message: {}", messageStr.strip()); | ||
} | ||
catch(Exception e){ | ||
log.error("Failed to handle IRC WebSocket message {}", messageStr, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onClose(int code, String reason, boolean remote){ | ||
try(var ignored = LogContext.empty().withSocketId(uuid)){ | ||
log.info("IRC WebSocket closed with code {}, from host {}, reason {}", code, remote, reason); | ||
listeners.forEach(l -> l.onWebSocketClosed(this, code, reason, remote)); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Exception e){ | ||
log.error("Error from IRC WebSocket", e); | ||
} | ||
|
||
private void onPing(){ | ||
lastPing = TimeFactory.now(); | ||
log.debug("Received IRC Ping request"); | ||
} | ||
|
||
private void sendMessage(@NotNull String message){ | ||
log.trace("Sending IRC message {}", message); | ||
send(message); | ||
} | ||
|
||
@Override | ||
public void onWebsocketPing(WebSocket conn, Framedata f){ | ||
onPing(); | ||
sendMessage("PONG"); | ||
} | ||
|
||
@Override | ||
public void join(@NotNull String channel){ | ||
try(var ignored = LogContext.empty().withSocketId(uuid)){ | ||
if(channels.add(channel)){ | ||
sendMessage("JOIN #" + channel); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void leave(@NotNull String channel){ | ||
try(var ignored = LogContext.empty().withSocketId(uuid)){ | ||
sendMessage("PART #" + channel); | ||
channels.remove(channel); | ||
} | ||
} | ||
|
||
public boolean isChannelJoined(@NotNull String channel){ | ||
return channels.contains(channel); | ||
} | ||
|
||
public void addListener(ITwitchChatWebSocketListener listener){ | ||
listeners.add(listener); | ||
} | ||
|
||
public long getChannelCount(){ | ||
return getChannels().size(); | ||
} | ||
} |
Oops, something went wrong.