Fix access levels of WSClient

- Change some public access modifiers to private
- Remove getSession and change it to checkSession that simply returns
boolean that checks if other session is equal with current and update
SessionManager according to that
- Fix naming of gson const (change it to GSON)

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
Tomas Slusny
2018-01-23 11:21:43 +01:00
committed by Adam
parent 8d81e82177
commit 7ee2c9d4b6
2 changed files with 11 additions and 13 deletions

View File

@@ -127,7 +127,7 @@ public class SessionManager
public void openSession(AccountSession session) public void openSession(AccountSession session)
{ {
// 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.getSession().equals(session)) if (wsclient == null || !wsclient.checkSession(session))
{ {
if (wsclient != null) if (wsclient != null)
{ {

View File

@@ -47,29 +47,27 @@ import okhttp3.WebSocketListener;
public class WSClient extends WebSocketListener implements AutoCloseable public class WSClient extends WebSocketListener implements AutoCloseable
{ {
private static final Duration PING_TIME = Duration.ofSeconds(30); private static final Duration PING_TIME = Duration.ofSeconds(30);
private static final Gson GSON = WebsocketGsonFactory.build();
private static final Gson gson = WebsocketGsonFactory.build();
private final OkHttpClient client = new OkHttpClient(); private final OkHttpClient client = new OkHttpClient();
private final EventBus eventBus; private final EventBus eventBus;
private final ScheduledFuture pingFuture;
private final AccountSession session; private final AccountSession session;
private WebSocket webSocket; private WebSocket webSocket;
private final ScheduledFuture pingFuture;
public WSClient(EventBus eventBus, ScheduledExecutorService executor, AccountSession session) WSClient(EventBus eventBus, ScheduledExecutorService executor, AccountSession session)
{ {
this.eventBus = eventBus; this.eventBus = eventBus;
this.session = session; this.session = session;
this.pingFuture = executor.scheduleWithFixedDelay(this::ping, PING_TIME.getSeconds(), PING_TIME.getSeconds(), TimeUnit.SECONDS); this.pingFuture = executor.scheduleWithFixedDelay(this::ping, PING_TIME.getSeconds(), PING_TIME.getSeconds(), TimeUnit.SECONDS);
} }
public AccountSession getSession() boolean checkSession(AccountSession session)
{ {
return session; return session.equals(this.session);
} }
public void connect() void connect()
{ {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(RuneLiteAPI.getWsEndpoint()) .url(RuneLiteAPI.getWsEndpoint())
@@ -82,14 +80,14 @@ public class WSClient extends WebSocketListener implements AutoCloseable
send(handshake); send(handshake);
} }
public void ping() private void ping()
{ {
Ping ping = new Ping(); Ping ping = new Ping();
ping.setTime(Instant.now()); ping.setTime(Instant.now());
send(ping); send(ping);
} }
public void send(WebsocketMessage message) private void send(WebsocketMessage message)
{ {
if (webSocket == null) if (webSocket == null)
{ {
@@ -98,7 +96,7 @@ public class WSClient extends WebSocketListener implements AutoCloseable
connect(); connect();
} }
String json = gson.toJson(message, WebsocketMessage.class); String json = GSON.toJson(message, WebsocketMessage.class);
webSocket.send(json); webSocket.send(json);
log.debug("Sent: {}", json); log.debug("Sent: {}", json);
@@ -127,7 +125,7 @@ public class WSClient extends WebSocketListener implements AutoCloseable
@Override @Override
public void onMessage(WebSocket webSocket, String text) public void onMessage(WebSocket webSocket, String text)
{ {
WebsocketMessage message = gson.fromJson(text, WebsocketMessage.class); WebsocketMessage message = GSON.fromJson(text, WebsocketMessage.class);
log.debug("Got message: {}", message); log.debug("Got message: {}", message);
eventBus.post(message); eventBus.post(message);