session manager: lazily open websocket

This commit is contained in:
Adam
2019-01-20 21:38:38 -05:00
parent 90711882e0
commit 63ca800ff7
2 changed files with 20 additions and 10 deletions

View File

@@ -101,7 +101,7 @@ public class SessionManager
return; return;
} }
openSession(session); openSession(session, false);
} }
private void saveSession() private void saveSession()
@@ -134,18 +134,21 @@ public class SessionManager
* *
* @param session session * @param session session
*/ */
private void openSession(AccountSession session) private void openSession(AccountSession session, boolean openSocket)
{ {
// If the ws session already exists, don't need to do anything // If the ws session already exists, don't need to do anything
if (wsclient == null || !wsclient.checkSession(session)) if (openSocket)
{ {
if (wsclient != null) if (wsclient == null || !wsclient.checkSession(session))
{ {
wsclient.close(); if (wsclient != null)
} {
wsclient.close();
}
wsclient = new WSClient(eventBus, executor, session); wsclient = new WSClient(eventBus, executor, session);
wsclient.connect(); wsclient.connect();
}
} }
accountSession = session; accountSession = session;
@@ -208,7 +211,7 @@ public class SessionManager
} }
// Create new session // Create new session
openSession(new AccountSession(login.getUid(), Instant.now())); openSession(new AccountSession(login.getUid(), Instant.now()), true);
// Navigate to login link // Navigate to login link
LinkBrowser.browse(login.getOauthUrl()); LinkBrowser.browse(login.getOauthUrl());
@@ -224,7 +227,8 @@ public class SessionManager
// Open session, again, now that we have a username // Open session, again, now that we have a username
// This triggers onSessionOpen // This triggers onSessionOpen
openSession(session); // The socket is already opened here anyway so we pass true for openSocket
openSession(session, true);
// Save session to disk // Save session to disk
saveSession(); saveSession();

View File

@@ -82,6 +82,12 @@ public class WSClient extends WebSocketListener implements AutoCloseable
private void ping() private void ping()
{ {
if (webSocket == null)
{
// Don't open a socket just for ping.
return;
}
Ping ping = new Ping(); Ping ping = new Ping();
ping.setTime(Instant.now()); ping.setTime(Instant.now());
send(ping); send(ping);