Skip to content

Commit 3add329

Browse files
Add GatewayPingEvent (discord-jda#1028)
Log raw close reason if the close code is not handled
1 parent 3c065b7 commit 3add329

File tree

5 files changed

+112
-5
lines changed

5 files changed

+112
-5
lines changed

Diff for: src/main/java/net/dv8tion/jda/api/JDA.java

+2
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ public boolean equals(Object o)
196196
* <p><b>{@link net.dv8tion.jda.api.requests.RestAction RestAction} request times do not
197197
* correlate to this value!</b>
198198
*
199+
* <p>The {@link net.dv8tion.jda.api.events.GatewayPingEvent GatewayPingEvent} indicates an update to this value.
200+
*
199201
* @return time in milliseconds between heartbeat and the heartbeat ack response
200202
*
201203
* @see #getRestPing() Getting RestAction ping
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2015-2019 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.dv8tion.jda.api.events;
18+
19+
import net.dv8tion.jda.api.JDA;
20+
21+
import javax.annotation.Nonnull;
22+
23+
/**
24+
* Indicates that the gateway ping has been updated by the heartbeat cycle.
25+
* <br>You can always get the last ping update with {@link net.dv8tion.jda.api.JDA#getGatewayPing()}.
26+
*
27+
* <p>Can be used to detect changes to the gateway ping.
28+
*
29+
* <p>Identifier: {@code gateway-ping}
30+
*/
31+
public class GatewayPingEvent extends Event implements UpdateEvent<JDA, Long>
32+
{
33+
public static final String IDENTIFIER = "gateway-ping";
34+
private final long next, prev;
35+
36+
public GatewayPingEvent(@Nonnull JDA api, long old)
37+
{
38+
super(api);
39+
this.next = api.getGatewayPing();
40+
this.prev = old;
41+
}
42+
43+
/**
44+
* The new ping for the current JDA session
45+
*
46+
* @return The new ping in milliseconds
47+
*/
48+
public long getNewPing()
49+
{
50+
return next;
51+
}
52+
53+
/**
54+
* The previous ping for the current JDA session
55+
*
56+
* @return The previous ping in milliseconds, or -1 if no ping was available yet
57+
*/
58+
public long getOldPing()
59+
{
60+
return prev;
61+
}
62+
63+
@Nonnull
64+
@Override
65+
public String getPropertyIdentifier()
66+
{
67+
return IDENTIFIER;
68+
}
69+
70+
@Nonnull
71+
@Override
72+
public JDA getEntity()
73+
{
74+
return getJDA();
75+
}
76+
77+
@Nonnull
78+
@Override
79+
public Long getOldValue()
80+
{
81+
return prev;
82+
}
83+
84+
@Nonnull
85+
@Override
86+
public Long getNewValue()
87+
{
88+
return next;
89+
}
90+
91+
@Override
92+
public String toString()
93+
{
94+
return "GatewayUpdate[ping](" + getOldValue() + "->" + getNewValue() + ')';
95+
}
96+
}

Diff for: src/main/java/net/dv8tion/jda/api/hooks/ListenerAdapter.java

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public abstract class ListenerAdapter implements EventListener
111111
public void onGenericEvent(@Nonnull GenericEvent event) {}
112112
public void onGenericUpdate(@Nonnull UpdateEvent<?, ?> event) {}
113113
public void onRawGateway(@Nonnull RawGatewayEvent event) {}
114+
public void onGatewayPing(@Nonnull GatewayPingEvent event) {}
114115

115116
//JDA Events
116117
public void onReady(@Nonnull ReadyEvent event) {}
@@ -335,6 +336,8 @@ else if (event instanceof StatusChangeEvent)
335336
onStatusChange((StatusChangeEvent) event);
336337
else if (event instanceof ExceptionEvent)
337338
onException((ExceptionEvent) event);
339+
else if (event instanceof GatewayPingEvent)
340+
onGatewayPing((GatewayPingEvent) event);
338341

339342
//Message Events
340343
//Guild (TextChannel) Message Events

Diff for: src/main/java/net/dv8tion/jda/internal/JDAImpl.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import net.dv8tion.jda.api.audio.factory.IAudioSendFactory;
2626
import net.dv8tion.jda.api.audio.hooks.ConnectionStatus;
2727
import net.dv8tion.jda.api.entities.*;
28+
import net.dv8tion.jda.api.events.GatewayPingEvent;
2829
import net.dv8tion.jda.api.events.GenericEvent;
2930
import net.dv8tion.jda.api.events.StatusChangeEvent;
3031
import net.dv8tion.jda.api.exceptions.AccountTypeException;
@@ -116,7 +117,7 @@ public class JDAImpl implements JDA
116117
protected SelfUser selfUser;
117118
protected ShardInfo shardInfo;
118119
protected long responseTotal;
119-
protected long ping = -1;
120+
protected long gatewayPing = -1;
120121
protected String gatewayUrl;
121122

122123
protected String clientId = null;
@@ -418,7 +419,7 @@ public Status getStatus()
418419
@Override
419420
public long getGatewayPing()
420421
{
421-
return ping;
422+
return gatewayPing;
422423
}
423424

424425
@Nonnull
@@ -835,9 +836,11 @@ public void setAudioSendFactory(IAudioSendFactory factory)
835836
this.audioSendFactory = factory;
836837
}
837838

838-
public void setPing(long ping)
839+
public void setGatewayPing(long ping)
839840
{
840-
this.ping = ping;
841+
long oldPing = this.gatewayPing;
842+
this.gatewayPing = ping;
843+
handleEvent(new GatewayPingEvent(this, oldPing));
841844
}
842845

843846
public Requester getRequester()

Diff for: src/main/java/net/dv8tion/jda/internal/requests/WebSocketClient.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,14 @@ public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame,
380380
if (serverCloseFrame != null)
381381
{
382382
rawCloseCode = serverCloseFrame.getCloseCode();
383+
String rawCloseReason = serverCloseFrame.getCloseReason();
383384
closeCode = CloseCode.from(rawCloseCode);
384385
if (closeCode == CloseCode.RATE_LIMITED)
385386
LOG.error("WebSocket connection closed due to ratelimit! Sent more than 120 websocket messages in under 60 seconds!");
386387
else if (closeCode != null)
387388
LOG.debug("WebSocket connection closed with code {}", closeCode);
389+
else if (rawCloseReason != null)
390+
LOG.warn("WebSocket connection closed with code {}: {}", rawCloseCode, rawCloseReason);
388391
else
389392
LOG.warn("WebSocket connection closed with unknown meaning for close-code {}", rawCloseCode);
390393
}
@@ -764,7 +767,7 @@ protected void onEvent(DataObject content)
764767
break;
765768
case WebSocketCode.HEARTBEAT_ACK:
766769
LOG.trace("Got Heartbeat Ack (OP 11).");
767-
api.setPing(System.currentTimeMillis() - heartbeatStartTime);
770+
api.setGatewayPing(System.currentTimeMillis() - heartbeatStartTime);
768771
break;
769772
default:
770773
LOG.debug("Got unknown op-code: {} with content: {}", opCode, content);

0 commit comments

Comments
 (0)