Search in sources :

Example 1 with IdentifyRequest

use of sx.blah.discord.api.internal.json.requests.IdentifyRequest 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);
    }
}
Also used : IdentifyRequest(sx.blah.discord.api.internal.json.requests.IdentifyRequest) DisconnectedEvent(sx.blah.discord.handle.impl.events.shard.DisconnectedEvent) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ResumeRequest(sx.blah.discord.api.internal.json.requests.ResumeRequest) ClosedChannelException(java.nio.channels.ClosedChannelException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)1 UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)1 IdentifyRequest (sx.blah.discord.api.internal.json.requests.IdentifyRequest)1 ResumeRequest (sx.blah.discord.api.internal.json.requests.ResumeRequest)1 DisconnectedEvent (sx.blah.discord.handle.impl.events.shard.DisconnectedEvent)1