use of org.javacord.api.event.connection.LostConnectionEvent in project Javacord by BtoBastian.
the class DiscordWebSocketAdapter method onDisconnected.
@Override
public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) {
Optional<WebSocketFrame> closeFrameOptional = Optional.ofNullable(closedByServer ? serverCloseFrame : clientCloseFrame);
String closeReason = closeFrameOptional.map(WebSocketFrame::getCloseReason).orElse("unknown");
String closeCodeString = closeFrameOptional.map(closeFrame -> {
int code = closeFrame.getCloseCode();
return WebSocketCloseCode.fromCode(code).map(closeCode -> closeCode + " (" + code + ")").orElseGet(() -> String.valueOf(code));
}).orElse("'unknown'");
logger.info("Websocket closed with reason '{}' and code {} by {}!", closeReason, closeCodeString, closedByServer ? "server" : "client");
LostConnectionEvent lostConnectionEvent = new LostConnectionEventImpl(api);
api.getEventDispatcher().dispatchLostConnectionEvent(null, lostConnectionEvent);
// Squash it, until it stops beating
heart.squash();
if (!ready.isDone()) {
ready.complete(false);
return;
}
// Reconnect
if (reconnect) {
reconnectingOrResumingLock.lock();
try {
reconnectAttempt.incrementAndGet();
} finally {
reconnectingOrResumingLock.unlock();
}
logger.info("Trying to reconnect/resume in {} seconds!", api.getReconnectDelay(reconnectAttempt.get()));
// Reconnect after a (short?) delay depending on the amount of reconnect attempts
api.getThreadPool().getScheduler().schedule(this::connect, api.getReconnectDelay(reconnectAttempt.get()), TimeUnit.SECONDS);
}
}
Aggregations