All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.rsocket.transport.netty.server.BaseWebsocketServerTransport Maven / Gradle / Ivy

There is a newer version: 1.1.4
Show newest version
package io.rsocket.transport.netty.server;

import static io.netty.channel.ChannelHandler.*;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.util.ReferenceCountUtil;
import io.rsocket.Closeable;
import io.rsocket.transport.ServerTransport;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.netty.http.server.HttpServer;

abstract class BaseWebsocketServerTransport implements ServerTransport {
  private static final Logger logger = LoggerFactory.getLogger(BaseWebsocketServerTransport.class);
  private static final ChannelHandler pongHandler = new PongHandler();

  static Function serverConfigurer =
      server ->
          server.tcpConfiguration(
              tcpServer ->
                  tcpServer.doOnConnection(connection -> connection.addHandlerLast(pongHandler)));

  @Sharable
  private static class PongHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
      if (msg instanceof PongWebSocketFrame) {
        logger.debug("received WebSocket Pong Frame");
        ReferenceCountUtil.safeRelease(msg);
        ctx.read();
      } else {
        ctx.fireChannelRead(msg);
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy