Search in sources :

Example 1 with PingFrame

use of org.httpkit.server.Frame.PingFrame in project http-kit by http-kit.

the class HttpServer method decodeWs.

private void decodeWs(WsAtta atta, SelectionKey key) {
    try {
        do {
            Frame frame = atta.decoder.decode(buffer);
            if (frame instanceof TextFrame || frame instanceof BinaryFrame) {
                handler.handle(atta.channel, frame);
                atta.decoder.reset();
            } else if (frame instanceof PingFrame) {
                handler.handle(atta.channel, frame);
                atta.decoder.reset();
                tryWrite(key, WsEncode(WSDecoder.OPCODE_PONG, frame.data));
            } else if (frame instanceof PongFrame) {
                // ignored as unsolicited pong frame from client
                atta.decoder.reset();
            } else if (frame instanceof CloseFrame) {
                // A snapshot
                boolean closed = atta.channel.isClosed();
                handler.clientClose(atta.channel, ((CloseFrame) frame).getStatus());
                // close the TCP connection after sent
                atta.keepalive = false;
                atta.decoder.reset();
                // Do not send CLOSE frame again if it has been sent.
                if (!closed) {
                    tryWrite(key, WsEncode(WSDecoder.OPCODE_CLOSE, frame.data));
                }
            }
        } while (// consume all
        buffer.hasRemaining());
    } catch (ProtocolException e) {
        warnLogger.log(null, e);
        eventLogger.log(eventNames.serverWsDecodeError);
        // TODO more specific error
        closeKey(key, CLOSE_MESG_BIG);
    }
}
Also used : PongFrame(org.httpkit.server.Frame.PongFrame) ProtocolException(org.httpkit.ProtocolException) CloseFrame(org.httpkit.server.Frame.CloseFrame) BinaryFrame(org.httpkit.server.Frame.BinaryFrame) PongFrame(org.httpkit.server.Frame.PongFrame) PingFrame(org.httpkit.server.Frame.PingFrame) TextFrame(org.httpkit.server.Frame.TextFrame) BinaryFrame(org.httpkit.server.Frame.BinaryFrame) PingFrame(org.httpkit.server.Frame.PingFrame) TextFrame(org.httpkit.server.Frame.TextFrame) CloseFrame(org.httpkit.server.Frame.CloseFrame)

Aggregations

ProtocolException (org.httpkit.ProtocolException)1 BinaryFrame (org.httpkit.server.Frame.BinaryFrame)1 CloseFrame (org.httpkit.server.Frame.CloseFrame)1 PingFrame (org.httpkit.server.Frame.PingFrame)1 PongFrame (org.httpkit.server.Frame.PongFrame)1 TextFrame (org.httpkit.server.Frame.TextFrame)1