Dispatch user status upon listen
Some checks failed
Publish JS SDK / docker (push) Failing after 14s
Deploy API / deploy (ubuntu-latest, 2.44.0) (push) Successful in 1m6s

This commit is contained in:
Braydon 2024-09-10 16:09:31 -04:00
parent 2f0b6d5982
commit 5d228bca06
2 changed files with 12 additions and 11 deletions

@ -57,7 +57,6 @@ public class WebSocket extends TextWebSocketHandler {
} }
for (WebSocketClient client : activeSessions.values()) { for (WebSocketClient client : activeSessions.values()) {
String sessionId = client.getSession().getId(); String sessionId = client.getSession().getId();
System.out.println("client.getSession().getId() = " + sessionId);
// Disconnect users that have not been active for 30 seconds // Disconnect users that have not been active for 30 seconds
if (client.getListeningTo() == null && ((System.currentTimeMillis() - client.getConnected()) >= TimeUnit.SECONDS.toMillis(30L))) { if (client.getListeningTo() == null && ((System.currentTimeMillis() - client.getConnected()) >= TimeUnit.SECONDS.toMillis(30L))) {
@ -66,20 +65,12 @@ public class WebSocket extends TextWebSocketHandler {
continue; continue;
} }
if (client.getListeningTo() == null) { if (client.getListeningTo() == null) {
System.err.println("NOT LISTENING!!!");
continue; continue;
} }
// Notify the listening client of the user's status if it has changed // Notify the listening client of the user's status if it has changed
try { try {
DiscordUser user = discordService.getUserBySnowflake(client.getListeningTo()).getUser(); dispatchUserStatus(client);
System.out.println("user = " + user);
if (!user.equals(client.getLastUser())) {
client.setLastUser(user);
dispatch(client.getSession(), new UserStatusPacket(user));
}
} catch (BadRequestException | ServiceUnavailableException | ResourceNotFoundException ex) { } catch (BadRequestException | ServiceUnavailableException | ResourceNotFoundException ex) {
System.err.println(ex.getLocalizedMessage());
System.err.println("STOPPED LISTENING TO USER!!!!!!!!!!!!!!");
client.setListeningTo(null); client.setListeningTo(null);
dispatch(client.getSession(), new ErrorMessagePacket(ex.getLocalizedMessage())); dispatch(client.getSession(), new ErrorMessagePacket(ex.getLocalizedMessage()));
} }
@ -129,8 +120,9 @@ public class WebSocket extends TextWebSocketHandler {
// Handle the packet // Handle the packet
if (packet instanceof ListenToUserPacket listenToUserPacket) { if (packet instanceof ListenToUserPacket listenToUserPacket) {
client.setListeningTo(listenToUserPacket.getSnowflake());
log.info("Session {} is listening to user updates for {}", sessionId, client.getListeningTo()); log.info("Session {} is listening to user updates for {}", sessionId, client.getListeningTo());
client.setListeningTo(listenToUserPacket.getSnowflake());
dispatchUserStatus(client); // Dispatch user status upon listen
} }
} catch (JsonSyntaxException ex) { // The syntax provided is invalid, close the session } catch (JsonSyntaxException ex) { // The syntax provided is invalid, close the session
session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Invalid payload")); session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Invalid payload"));
@ -151,6 +143,14 @@ public class WebSocket extends TextWebSocketHandler {
log.info("Session closed ({}): {}", status, sessionId); log.info("Session closed ({}): {}", status, sessionId);
} }
private void dispatchUserStatus(@NonNull WebSocketClient client) {
DiscordUser user = discordService.getUserBySnowflake(client.getListeningTo()).getUser();
if (!user.equals(client.getLastUser())) {
client.setLastUser(user);
dispatch(client.getSession(), new UserStatusPacket(user));
}
}
/** /**
* Send a packet to the given session. * Send a packet to the given session.
* *

@ -17,6 +17,7 @@ export const useTetherWS = (
config: TetherConfig = { config: TetherConfig = {
endpoint: "usetether.rest", endpoint: "usetether.rest",
secure: true, secure: true,
debug: false,
} }
): DiscordUser | undefined => { ): DiscordUser | undefined => {
const { endpoint, secure, debug } = config; const { endpoint, secure, debug } = config;