类 io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler 源码实例Demo

下面列出了怎么用 io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler 的API类实例代码及写法,或者点击链接到github查看源代码。


@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new IdleStateHandler(0, 0, 60));
    pipeline.addLast(new WebSocketServerProtocolHandler(WEB_SOCKET_PATH, null, true));
    pipeline.addLast(new WebSocketHandler(litchi));

    for (ChannelHandler handler : handlers) {
        pipeline.addLast(handler);
    }
}
 

@Override
protected void initChannel(SocketChannel ch) {
  if (sslCtx != null) {
    ch.pipeline().addLast("ssl", sslCtx.newHandler(ch.alloc()));
  }
  ch.pipeline().addLast("codec", new HttpServerCodec());
  ch.pipeline().addLast("keep-alive", new HttpServerKeepAliveHandler());
  ch.pipeline().addLast("chunked-write", new ChunkedWriteHandler());

  // Websocket magic
  ch.pipeline().addLast("ws-compression", new WebSocketServerCompressionHandler());
  ch.pipeline().addLast("ws-protocol", new WebSocketUpgradeHandler(KEY, webSocketHandler));
  ch.pipeline().addLast("netty-to-se-messages", new MessageInboundConverter());
  ch.pipeline().addLast("se-to-netty-messages", new MessageOutboundConverter());
  ch.pipeline().addLast("se-websocket-handler", new WebSocketMessageHandler(KEY));

  // Regular HTTP magic
  ch.pipeline().addLast("se-request", new RequestConverter());
  ch.pipeline().addLast("se-response", new ResponseConverter());
  ch.pipeline().addLast("se-handler", new SeleniumHandler(seleniumHandler));
}
 

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, subProtocols, true));
    pipeline.addLast(new WebSocketRemoteServerFrameHandler());
}
 

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
    pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
    pipeline.addLast(new WebSocketFrameHandler());
}
 
源代码5 项目: Launcher   文件: LauncherNettyServer.java

public LauncherNettyServer(LaunchServer server) {
    LaunchServerConfig.NettyConfig config = server.config.netty;
    NettyObjectFactory.setUsingEpoll(config.performance.usingEpoll);
    if (config.performance.usingEpoll) {
        LogHelper.debug("Netty: Epoll enabled");
    }
    if (config.performance.usingEpoll && !Epoll.isAvailable()) {
        LogHelper.error("Epoll is not available: (netty,perfomance.usingEpoll configured wrongly)", Epoll.unavailabilityCause());
    }
    bossGroup = NettyObjectFactory.newEventLoopGroup(config.performance.bossThread);
    workerGroup = NettyObjectFactory.newEventLoopGroup(config.performance.workerThread);
    serverBootstrap = new ServerBootstrap();
    service = new WebSocketService(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), server);
    serverBootstrap.group(bossGroup, workerGroup)
            .channelFactory(NettyObjectFactory.getServerSocketChannelFactory())
            .handler(new LoggingHandler(config.logLevel))
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) {
                    ChannelPipeline pipeline = ch.pipeline();
                    NettyConnectContext context = new NettyConnectContext();
                    //p.addLast(new LoggingHandler(LogLevel.INFO));
                    pipeline.addLast("http-codec", new HttpServerCodec());
                    pipeline.addLast("http-codec-compressor", new HttpObjectAggregator(65536));
                    if (server.config.netty.ipForwarding)
                        pipeline.addLast("forward-http", new NettyIpForwardHandler(context));
                    pipeline.addLast("websock-comp", new WebSocketServerCompressionHandler());
                    pipeline.addLast("websock-codec", new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
                    if (server.config.netty.fileServerEnabled)
                        pipeline.addLast("fileserver", new FileServerHandler(server.updatesDir, true, config.showHiddenFiles));
                    pipeline.addLast("launchserver", new WebSocketFrameHandler(context, server, service));
                    pipelineHook.hook(context, ch);
                }
            });
}
 

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true, 65536, false, true, 10000L));

    pipeline.addLast(new TunnelSocketFrameHandler(tunnelServer));
}
 

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
    pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
    pipeline.addLast(new WebSocketFrameHandler());
}
 
源代码8 项目: mpush   文件: WebsocketServer.java

@Override
protected void initPipeline(ChannelPipeline pipeline) {
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(CC.mp.net.ws_path, null, true));
    pipeline.addLast(new WebSocketIndexPageHandler());
    pipeline.addLast(getChannelHandler());
}
 

/**
 * 채널 파이프라인 설정.
 * Netty.Server.Configuration.NettyServerConfiguration 에서 등록한 Bean 을 이용해 사용자의 통신을 처리할 Handler 도 등록.
 * Netty.Server.Handler.JsonHandler 에서 실제 사용자 요청 처리.
 *
 * @param channel
 * @throws Exception
 */
@Override
protected void initChannel(Channel channel) throws Exception {

	ChannelPipeline channelPipeline = channel.pipeline();

	switch (transferType) {

		case "websocket":

			channelPipeline
					.addLast(new HttpServerCodec())
					.addLast(new HttpObjectAggregator(65536))
					.addLast(new WebSocketServerCompressionHandler())
					.addLast(new WebSocketServerProtocolHandler(transferWebsocketPath, transferWebsocketSubProtocol, transferWebsocketAllowExtensions))
					.addLast(new LoggingHandler(LogLevel.valueOf(logLevelPipeline)))
					.addLast(websocketHandler);

		case "tcp":
		default:

			channelPipeline
					.addLast(new LineBasedFrameDecoder(Integer.MAX_VALUE))
					.addLast(STRING_DECODER)
					.addLast(STRING_ENCODER)
					.addLast(new LoggingHandler(LogLevel.valueOf(logLevelPipeline)))
					.addLast(jsonHandler);

	}

}
 

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, subProtocols, true));
    pipeline.addLast(new WebSocketRemoteServerFrameHandler());
}
 

@Override
protected void addPushHandlers(final ChannelPipeline pipeline) {
    pipeline.addLast(PushAuthHandler.NAME, pushAuthHandler);
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(PushProtocol.WEBSOCKET.getPath(), null, true));
    pipeline.addLast(new PushRegistrationHandler(pushConnectionRegistry, PushProtocol.WEBSOCKET));
    pipeline.addLast(new SampleWebSocketPushClientProtocolHandler());
}
 

@SuppressWarnings("FutureReturnValueIgnored")
WebsocketServerOperations(String wsUrl, WebsocketServerSpec websocketServerSpec, HttpServerOperations replaced) {
	super(replaced);
	this.proxyPing = websocketServerSpec.handlePing();

	Channel channel = replaced.channel();
	onCloseState = MonoProcessor.create();

	// Handshake
	WebSocketServerHandshakerFactory wsFactory =
			new WebSocketServerHandshakerFactory(wsUrl, websocketServerSpec.protocols(), true, websocketServerSpec.maxFramePayloadLength());
	handshaker = wsFactory.newHandshaker(replaced.nettyRequest);
	if (handshaker == null) {
		//"FutureReturnValueIgnored" this is deliberate
		WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel);
		handshakerResult = null;
	}
	else {
		removeHandler(NettyPipeline.HttpTrafficHandler);
		removeHandler(NettyPipeline.AccessLogHandler);
		removeHandler(NettyPipeline.HttpMetricsHandler);

		handshakerResult = channel.newPromise();
		HttpRequest request = new DefaultFullHttpRequest(replaced.version(),
				replaced.method(),
				replaced.uri());

		request.headers()
		       .set(replaced.nettyRequest.headers());

		if (websocketServerSpec.compress()) {
			removeHandler(NettyPipeline.CompressionHandler);

			WebSocketServerCompressionHandler wsServerCompressionHandler =
					new WebSocketServerCompressionHandler();
			try {
				wsServerCompressionHandler.channelRead(channel.pipeline()
				                                              .context(NettyPipeline.ReactiveBridge),
						request);

				addHandlerFirst(NettyPipeline.WsCompressionHandler, wsServerCompressionHandler);
			} catch (Throwable e) {
				log.error(format(channel(), ""), e);
			}
		}

		handshaker.handshake(channel,
		                     request,
		                     replaced.responseHeaders
		                             .remove(HttpHeaderNames.TRANSFER_ENCODING),
		                     handshakerResult)
		          .addListener(f -> {
		              if (replaced.rebind(this)) {
		                  markPersistent(false);
		              }
		              else {
		                  log.debug("Cannot bind WebsocketServerOperations after the handshake.");
		              }
		          });
	}
}
 

@Override
protected void initChannel(Channel ch) throws Exception {
  ChannelPipeline pipeline = ch.pipeline();
  // Flash policy file
  if (isFlashSupported) {
    pipeline.addLast(FLASH_POLICY_HANDLER, flashPolicyHandler);
  }
  // SSL
  if (sslContext != null) {
    pipeline.addLast(SSL_HANDLER, sslContext.newHandler(ch.alloc()));
  }

  // HTTP
  pipeline.addLast(HTTP_REQUEST_DECODER, new HttpRequestDecoder());
  pipeline.addLast(HTTP_CHUNK_AGGREGATOR, new HttpObjectAggregator(MAX_HTTP_CONTENT_LENGTH));
  pipeline.addLast(HTTP_RESPONSE_ENCODER, new HttpResponseEncoder());
  if (isHttpCompressionEnabled) {
    pipeline.addLast(HTTP_COMPRESSION, new HttpContentCompressor());
  }

  // Flash resources
  if (isFlashSupported) {
    pipeline.addLast(FLASH_RESOURCE_HANDLER, flashResourceHandler);
  }

  // Socket.IO
  pipeline.addLast(SOCKETIO_PACKET_ENCODER, packetEncoderHandler);
  pipeline.addLast(SOCKETIO_HANDSHAKE_HANDLER, handshakeHandler);
  pipeline.addLast(SOCKETIO_DISCONNECT_HANDLER, disconnectHandler);
  if (isWebsocketCompressionEnabled) {
    pipeline.addLast(WEBSOCKET_COMPRESSION, new WebSocketServerCompressionHandler());
  }
  pipeline.addLast(SOCKETIO_WEBSOCKET_HANDLER, webSocketHandler);
  if (isFlashSupported) {
    pipeline.addLast(SOCKETIO_FLASHSOCKET_HANDLER, flashSocketHandler);
  }
  pipeline.addLast(SOCKETIO_XHR_POLLING_HANDLER, xhrPollingHandler);
  if (isJsonpSupported) {
    pipeline.addLast(SOCKETIO_JSONP_POLLING_HANDLER, jsonpPollingHandler);
  }
  pipeline.addLast(SOCKETIO_HEARTBEAT_HANDLER, heartbeatHandler);
  pipeline.addLast(eventExecutorGroup, SOCKETIO_PACKET_DISPATCHER, packetDispatcherHandler);

  if (pipelineModifier != null) {
    pipelineModifier.modifyPipeline(pipeline);
  }
}
 
 同包方法