use of sx.blah.discord.api.internal.json.requests.ResumeRequest in project Discord4J by Discord4J.
the class DiscordWS method onWebSocketText.
@Override
public void onWebSocketText(String message) {
try {
if (Discord4J.LOGGER.isTraceEnabled(LogMarkers.WEBSOCKET_TRAFFIC)) {
Discord4J.LOGGER.trace(LogMarkers.WEBSOCKET_TRAFFIC, "Received: " + message);
}
JsonNode json = DiscordUtils.MAPPER.readTree(message);
GatewayOps op = GatewayOps.get(json.get("op").asInt());
JsonNode d = json.has("d") && !json.get("d").isNull() ? json.get("d") : null;
if (json.has("s") && !json.get("s").isNull())
seq = json.get("s").longValue();
switch(op) {
case HELLO:
Discord4J.LOGGER.trace(LogMarkers.WEBSOCKET, "Shard {} _trace: {}", shard.getInfo()[0], d.get("_trace").toString());
heartbeatHandler.begin(d.get("heartbeat_interval").intValue());
if (this.state != State.RESUMING) {
send(GatewayOps.IDENTIFY, new IdentifyRequest(client.getToken(), shard.getInfo(), identifyPresence));
} else {
client.reconnectManager.onReconnectSuccess();
send(GatewayOps.RESUME, new ResumeRequest(client.getToken(), sessionId, seq));
}
break;
case RECONNECT:
this.state = State.RESUMING;
client.getDispatcher().dispatch(new DisconnectedEvent(DisconnectedEvent.Reason.RECONNECT_OP, shard));
heartbeatHandler.shutdown();
send(GatewayOps.RESUME, new ResumeRequest(client.getToken(), sessionId, seq));
break;
case DISPATCH:
try {
dispatchHandler.handle(json);
} catch (Exception e) {
Discord4J.LOGGER.error(LogMarkers.WEBSOCKET, "Discord4J Internal Exception", e);
}
break;
case INVALID_SESSION:
this.state = State.RECONNECTING;
client.getDispatcher().dispatch(new DisconnectedEvent(DisconnectedEvent.Reason.INVALID_SESSION_OP, shard));
invalidate();
// TODO: try to maintain previous presence?
send(GatewayOps.IDENTIFY, new IdentifyRequest(client.getToken(), shard.getInfo(), null));
break;
case HEARTBEAT:
send(GatewayOps.HEARTBEAT, seq);
case HEARTBEAT_ACK:
heartbeatHandler.ack();
break;
case UNKNOWN:
Discord4J.LOGGER.debug(LogMarkers.WEBSOCKET, "Received unknown opcode, {}", message);
break;
}
} catch (IOException e) {
Discord4J.LOGGER.error(LogMarkers.WEBSOCKET, "JSON Parsing exception!", e);
}
}
Aggregations