具有不同服务路径的多个通道

时间:2017-09-21 07:26:56

标签: java websocket netty

[我正在使用Netty-Websokcet]

我有一个用例,其中不同的服务路径应该连接到同一个端口。我尝试了很多不同的方式,原因是我无法完成工作,

  • ServerBootstrap课程中ChannelHandler只有一个地方,因此我无法在ServerBootstrap中添加多个具有不同服务路径的子处理程序
  • ServerBootstrap课程中,无法创建多个小组

这是我的初始频道的样子,

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    logger.debug(1, "Initializing the SocketChannel : {}", socketChannel.id());

    socketChannel.pipeline().addLast(
            new HttpRequestDecoder(),
            new HttpObjectAggregator(maxPayloadSize),
            new HttpResponseEncoder(),
            new IdleStateHandler(0, 0, listenerConfig.getSocketTimeout(),
                    TimeUnit.SECONDS),
            new WebSocketServerProtocolHandler(ingressConfig.getURI().getPath()), // (A)
            new WebSocketServerCompressionHandler(),
            new WebSocketIO(listenerConfig, manager), // a handler
            new WebSocketMessageListener(messageReceiver, manager) // a handler
    );

    logger.debug(2, "Successfully initialized the Socket Channel : {}", socketChannel.id());
}

此代码行(A)使用给定的服务路径注册处理程序(服务路径为ingressConfig.getURI().getPath()

 int maxPayloadSize = listenerConfig.getMaxPayloadSize();
    try {
        bossGroup = new NioEventLoopGroup(listenerConfig.getBossThreadCount());
        workerGroup = new NioEventLoopGroup(listenerConfig.getWorkerThreadCount());

        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new WebSocketListenerInitializer(messageReceiver, maxPayloadSize, listenerConfig,
                        ingressConfig))
                .option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture channelFuture = bootstrap.bind(port);
        channelFuture.sync();
        channel = channelFuture.channel();

        if (channelFuture.isSuccess()) {
            logger.info(1, "WebSocket listener started on port : {} successfully", port);
        } else {
            logger.error(2, "Failed to start WebSocket server on port : {}", port,
                    channelFuture.cause());
            throw new TransportException("Failed to start WebSocket server", channelFuture.cause());
        }

    } catch (InterruptedException ex) {
        logger.error(1, "Interrupted Exception from : {}", WebSocketListener.class);
        throw new TransportException("Interrupted Exception", ex);
    }

任何人都可以建议我如何做到这一点吗?

0 个答案:

没有答案