Skip to content

Commit 2181279

Browse files
committed
Remove redundant websocket for sub-sessions
1 parent bbbb112 commit 2181279

3 files changed

Lines changed: 17 additions & 22 deletions

File tree

core/src/main/java/com/rtm516/mcxboxbroadcast/core/FriendManager.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ public boolean remove(String xuid) {
154154
public void initAutoFriend(FriendSyncConfig friendSyncConfig) {
155155
if (friendSyncConfig.autoFollow() || friendSyncConfig.autoUnfollow()) {
156156
sessionManager.scheduledThread().scheduleWithFixedDelay(() -> {
157-
// Make sure the connection is still active
158-
sessionManager.checkConnection();
159-
160157
// Auto Friend Checker
161158
try {
162159
for (FollowerResponse.Person person : get(friendSyncConfig.autoFollow(), friendSyncConfig.autoUnfollow())) {

core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public void updateSession(SessionInfo sessionInfo) throws SessionUpdateException
7878

7979
@Override
8080
protected void updateSession() throws SessionUpdateException {
81+
// Make sure the websocket connection is still active
82+
checkConnection();
83+
8184
super.updateSessionInternal(Constants.CREATE_SESSION.formatted(this.sessionInfo.getSessionId()), new CreateSessionRequest(this.sessionInfo));
8285
}
8386

core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManagerCore.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -179,25 +179,23 @@ private void createSession() throws SessionCreationException, SessionUpdateExcep
179179
XboxTokenInfo tokenInfo = getXboxToken();
180180
String token = tokenInfo.tokenHeader();
181181

182-
// Update the current session infos XUID
182+
// We only need a websocket for the primary session manager
183183
if (this.sessionInfo != null) {
184+
// Update the current session XUID
184185
this.sessionInfo.setXuid(tokenInfo.userXUID());
185-
}
186186

187-
// Create the RTA websocket connection
188-
setupWebsocket(token);
187+
// Create the RTA websocket connection
188+
setupWebsocket(token);
189189

190-
// Wait and get the connection ID from the websocket
191-
String connectionId;
192-
try {
193-
connectionId = waitForConnectionId().get();
194-
} catch (InterruptedException | ExecutionException e) {
195-
throw new SessionCreationException("Unable to get connectionId for session: " + e.getMessage());
196-
}
190+
try {
191+
// Wait and get the connection ID from the websocket
192+
String connectionId = waitForConnectionId().get();
197193

198-
// Update the current session infos connection ID
199-
if (this.sessionInfo != null) {
200-
this.sessionInfo.setConnectionId(connectionId);
194+
// Update the current session connection ID
195+
this.sessionInfo.setConnectionId(connectionId);
196+
} catch (InterruptedException | ExecutionException e) {
197+
throw new SessionCreationException("Unable to get connectionId for session: " + e.getMessage());
198+
}
201199
}
202200

203201
// Push the session information to the session directory
@@ -264,9 +262,6 @@ private void createSession() throws SessionCreationException, SessionUpdateExcep
264262
* @throws SessionUpdateException If the update fails
265263
*/
266264
protected void updateSessionInternal(String url, Object data) throws SessionUpdateException {
267-
// Make sure the websocket connection is still active
268-
checkConnection();
269-
270265
HttpRequest createSessionRequest;
271266
try {
272267
createSessionRequest = HttpRequest.newBuilder()
@@ -297,8 +292,8 @@ protected void updateSessionInternal(String url, Object data) throws SessionUpda
297292
* Check the connection to the websocket and if its closed re-open it and re-create the session
298293
* This should be called before any updates to the session otherwise they might fail
299294
*/
300-
public void checkConnection() {
301-
if (!rtaWebsocket.isOpen()) {
295+
protected void checkConnection() {
296+
if (this.rtaWebsocket != null && !rtaWebsocket.isOpen()) {
302297
try {
303298
logger.info("Connection to websocket lost, re-creating session...");
304299
createSession();

0 commit comments

Comments
 (0)