Search in sources :

Example 1 with LostConnectionEvent

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);
    }
}
Also used : ResumeEventImpl(org.javacord.core.event.connection.ResumeEventImpl) UserUpdateHandler(org.javacord.core.util.handler.user.UserUpdateHandler) RestMethod(org.javacord.core.util.rest.RestMethod) TypingStartHandler(org.javacord.core.util.handler.user.TypingStartHandler) Proxy(java.net.Proxy) ThreadMembersUpdateHandler(org.javacord.core.util.handler.channel.thread.ThreadMembersUpdateHandler) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) NvWebSocketRouteImpl(org.javacord.core.util.auth.NvWebSocketRouteImpl) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ThreadListSyncHandler(org.javacord.core.util.handler.channel.thread.ThreadListSyncHandler) Activity(org.javacord.api.entity.activity.Activity) MessageUpdateHandler(org.javacord.core.util.handler.message.MessageUpdateHandler) BlockingQueue(java.util.concurrent.BlockingQueue) Logger(org.apache.logging.log4j.Logger) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) ChannelDeleteHandler(org.javacord.core.util.handler.channel.ChannelDeleteHandler) WebSocketFactory(com.neovisionaries.ws.client.WebSocketFactory) MessageCreateHandler(org.javacord.core.util.handler.message.MessageCreateHandler) NvWebSocketResponseImpl(org.javacord.core.util.auth.NvWebSocketResponseImpl) InviteDeleteHandler(org.javacord.core.util.handler.channel.invite.InviteDeleteHandler) ThreadUpdateHandler(org.javacord.core.util.handler.channel.thread.ThreadUpdateHandler) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ChannelPinsUpdateHandler(org.javacord.core.util.handler.channel.ChannelPinsUpdateHandler) ArrayList(java.util.ArrayList) GuildMembersChunkHandler(org.javacord.core.util.handler.guild.GuildMembersChunkHandler) PasswordAuthentication(java.net.PasswordAuthentication) VoiceServerUpdateHandler(org.javacord.core.util.handler.guild.VoiceServerUpdateHandler) MessageReactionAddHandler(org.javacord.core.util.handler.message.reaction.MessageReactionAddHandler) GuildMemberAddHandler(org.javacord.core.util.handler.guild.GuildMemberAddHandler) GuildRoleUpdateHandler(org.javacord.core.util.handler.guild.role.GuildRoleUpdateHandler) DiscordApiImpl(org.javacord.core.DiscordApiImpl) PresenceUpdateHandler(org.javacord.core.util.handler.user.PresenceUpdateHandler) LoggerUtil(org.javacord.core.util.logging.LoggerUtil) Lock(java.util.concurrent.locks.Lock) Intent(org.javacord.api.entity.intent.Intent) User(org.javacord.api.entity.user.User) GuildBanAddHandler(org.javacord.core.util.handler.guild.GuildBanAddHandler) WebhooksUpdateHandler(org.javacord.core.util.handler.channel.WebhooksUpdateHandler) GuildMemberRemoveHandler(org.javacord.core.util.handler.guild.GuildMemberRemoveHandler) SocketAddress(java.net.SocketAddress) GuildRoleDeleteHandler(org.javacord.core.util.handler.guild.role.GuildRoleDeleteHandler) URL(java.net.URL) ChannelCreateHandler(org.javacord.core.util.handler.channel.ChannelCreateHandler) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) ProxySelector(java.net.ProxySelector) Authenticator(org.javacord.api.util.auth.Authenticator) GuildBanRemoveHandler(org.javacord.core.util.handler.guild.GuildBanRemoveHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GuildUpdateHandler(org.javacord.core.util.handler.guild.GuildUpdateHandler) RestRequest(org.javacord.core.util.rest.RestRequest) InteractionCreateHandler(org.javacord.core.util.handler.interaction.InteractionCreateHandler) URI(java.net.URI) GuildEmojisUpdateHandler(org.javacord.core.util.handler.guild.GuildEmojisUpdateHandler) ServerImpl(org.javacord.core.entity.server.ServerImpl) GuildDeleteHandler(org.javacord.core.util.handler.guild.GuildDeleteHandler) ThreadCreateHandler(org.javacord.core.util.handler.channel.thread.ThreadCreateHandler) GuildStickersUpdateHandler(org.javacord.core.util.handler.guild.GuildStickersUpdateHandler) AtomicMarkableReference(java.util.concurrent.atomic.AtomicMarkableReference) InetSocketAddress(java.net.InetSocketAddress) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ServerVoiceChannel(org.javacord.api.entity.channel.ServerVoiceChannel) MessageReactionRemoveAllHandler(org.javacord.core.util.handler.message.reaction.MessageReactionRemoveAllHandler) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) List(java.util.List) LostConnectionEvent(org.javacord.api.event.connection.LostConnectionEvent) WebSocketListener(com.neovisionaries.ws.client.WebSocketListener) WebSocketException(com.neovisionaries.ws.client.WebSocketException) Optional(java.util.Optional) ResumeEvent(org.javacord.api.event.connection.ResumeEvent) ResumedHandler(org.javacord.core.util.handler.ResumedHandler) ReadyHandler(org.javacord.core.util.handler.ReadyHandler) GuildMemberUpdateHandler(org.javacord.core.util.handler.guild.GuildMemberUpdateHandler) Nameable(org.javacord.api.entity.Nameable) VoiceStateUpdateHandler(org.javacord.core.util.handler.guild.VoiceStateUpdateHandler) Request(org.javacord.api.util.auth.Request) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PresencesReplaceHandler(org.javacord.core.util.handler.user.PresencesReplaceHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GuildCreateHandler(org.javacord.core.util.handler.guild.GuildCreateHandler) ProxySettings(com.neovisionaries.ws.client.ProxySettings) ReconnectEvent(org.javacord.api.event.connection.ReconnectEvent) DataFormatException(java.util.zip.DataFormatException) TrustAllTrustManager(org.javacord.core.util.http.TrustAllTrustManager) ThreadDeleteHandler(org.javacord.core.util.handler.channel.thread.ThreadDeleteHandler) ReconnectEventImpl(org.javacord.core.event.connection.ReconnectEventImpl) WeakHashMap(java.util.WeakHashMap) ExecutorService(java.util.concurrent.ExecutorService) MessageDeleteBulkHandler(org.javacord.core.util.handler.message.MessageDeleteBulkHandler) ReentrantLock(java.util.concurrent.locks.ReentrantLock) WebSocketFrame(com.neovisionaries.ws.client.WebSocketFrame) LostConnectionEventImpl(org.javacord.core.event.connection.LostConnectionEventImpl) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) WebSocketLogger(org.javacord.core.util.logging.WebSocketLogger) ChannelUpdateHandler(org.javacord.core.util.handler.channel.ChannelUpdateHandler) MessageReactionRemoveHandler(org.javacord.core.util.handler.message.reaction.MessageReactionRemoveHandler) WebSocketAdapter(com.neovisionaries.ws.client.WebSocketAdapter) Javacord(org.javacord.api.Javacord) TimeUnit(java.util.concurrent.TimeUnit) InviteCreateHandler(org.javacord.core.util.handler.channel.invite.InviteCreateHandler) Condition(java.util.concurrent.locks.Condition) MessageDeleteHandler(org.javacord.core.util.handler.message.MessageDeleteHandler) Server(org.javacord.api.entity.server.Server) WebSocket(com.neovisionaries.ws.client.WebSocket) GuildRoleCreateHandler(org.javacord.core.util.handler.guild.role.GuildRoleCreateHandler) Collections(java.util.Collections) LostConnectionEventImpl(org.javacord.core.event.connection.LostConnectionEventImpl) LostConnectionEvent(org.javacord.api.event.connection.LostConnectionEvent) WebSocketFrame(com.neovisionaries.ws.client.WebSocketFrame)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ProxySettings (com.neovisionaries.ws.client.ProxySettings)1 WebSocket (com.neovisionaries.ws.client.WebSocket)1 WebSocketAdapter (com.neovisionaries.ws.client.WebSocketAdapter)1 WebSocketException (com.neovisionaries.ws.client.WebSocketException)1 WebSocketFactory (com.neovisionaries.ws.client.WebSocketFactory)1 WebSocketFrame (com.neovisionaries.ws.client.WebSocketFrame)1 WebSocketListener (com.neovisionaries.ws.client.WebSocketListener)1 InetSocketAddress (java.net.InetSocketAddress)1 PasswordAuthentication (java.net.PasswordAuthentication)1 Proxy (java.net.Proxy)1 ProxySelector (java.net.ProxySelector)1 SocketAddress (java.net.SocketAddress)1 URI (java.net.URI)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1