use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project netty by netty.
the class WebSocketServerProtocolHandlerTest method createClient.
private EmbeddedChannel createClient(ChannelHandler... handlers) throws Exception {
WebSocketClientProtocolConfig clientConfig = WebSocketClientProtocolConfig.newBuilder().webSocketUri("http://test/test").dropPongFrames(false).handleCloseFrames(false).build();
EmbeddedChannel ch = new EmbeddedChannel(false, false, new HttpClientCodec(), new HttpObjectAggregator(8192), new WebSocketClientProtocolHandler(clientConfig));
ch.pipeline().addLast(handlers);
ch.register();
return ch;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project netty by netty.
the class WebSocketClient method main.
public static void main(String[] args) throws Exception {
URI uri = new URI(URL);
String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
final int port;
if (uri.getPort() == -1) {
if ("ws".equalsIgnoreCase(scheme)) {
port = 80;
} else if ("wss".equalsIgnoreCase(scheme)) {
port = 443;
} else {
port = -1;
}
} else {
port = uri.getPort();
}
if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
System.err.println("Only WS(S) is supported.");
return;
}
final boolean ssl = "wss".equalsIgnoreCase(scheme);
final SslContext sslCtx;
if (ssl) {
sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} else {
sslCtx = null;
}
EventLoopGroup group = new NioEventLoopGroup();
try {
// Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
// If you change it to V00, ping is not supported and remember to change
// HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()));
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
}
p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), WebSocketClientCompressionHandler.INSTANCE, handler);
}
});
Channel ch = b.connect(uri.getHost(), port).sync().channel();
handler.handshakeFuture().sync();
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String msg = console.readLine();
if (msg == null) {
break;
} else if ("bye".equals(msg.toLowerCase())) {
ch.writeAndFlush(new CloseWebSocketFrame());
ch.closeFuture().sync();
break;
} else if ("ping".equals(msg.toLowerCase())) {
WebSocketFrame frame = new PingWebSocketFrame(Unpooled.wrappedBuffer(new byte[] { 8, 1, 8, 1 }));
ch.writeAndFlush(frame);
} else {
WebSocketFrame frame = new TextWebSocketFrame(msg);
ch.writeAndFlush(frame);
}
}
} finally {
group.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project undertow by undertow-io.
the class WebSocketTestClient method connect.
/**
* Connect the WebSocket client
*
* @throws Exception
*/
public WebSocketTestClient connect() throws Exception {
String protocol = uri.getScheme();
if (!"ws".equals(protocol)) {
throw new IllegalArgumentException("Unsupported protocol: " + protocol);
}
final WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, version, null, false, new DefaultHttpHeaders());
WSClientHandler handler = new WSClientHandler(handshaker);
EventLoopGroup group = new NioEventLoopGroup();
bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer() {
@Override
protected void initChannel(Channel channel) throws Exception {
ChannelPipeline p = channel.pipeline();
p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), handler);
}
});
// Connect
ChannelFuture future = bootstrap.connect(new InetSocketAddress(uri.getHost(), uri.getPort()));
future.syncUninterruptibly();
handler.handshakeFuture.syncUninterruptibly();
ch = future.channel();
return this;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project rest.li by linkedin.
the class HttpChannelInitializer method initChannel.
@Override
protected void initChannel(NioSocketChannel channel) {
if (_ssl) {
channel.pipeline().addLast(SessionResumptionSslHandler.PIPELINE_SESSION_RESUMPTION_HANDLER, new SessionResumptionSslHandler(_sslContext, _sslParameters, _enableSSLSessionResumption, _sslHandShakeTimeout));
}
channel.pipeline().addLast("codec", new HttpClientCodec(_maxInitialLineLength, _maxHeaderSize, _maxChunkSize));
channel.pipeline().addLast("outboundRestRequestEncoder", HttpMessageEncoders.newRestRequestEncoder());
channel.pipeline().addLast("outboundStreamDataEncoder", HttpMessageEncoders.newDataEncoder());
channel.pipeline().addLast("outboundStreamRequestEncoder", HttpMessageEncoders.newStreamRequestEncoder());
channel.pipeline().addLast("inboundDataDecoder", HttpMessageDecoders.newDataDecoder());
channel.pipeline().addLast("inboundRequestDecoder", HttpMessageDecoders.newResponseDecoder());
channel.pipeline().addLast("schemeHandler", new SchemeHandler(_ssl ? HttpScheme.HTTPS.toString() : HttpScheme.HTTP.toString()));
channel.pipeline().addLast("streamDuplexHandler", new ClientEntityStreamHandler(_maxContentLength));
channel.pipeline().addLast("timeoutHandler", new CancelTimeoutHandler());
channel.pipeline().addLast("channelPoolHandler", new ChannelLifecycleHandler(RECYCLE_CHANNEL));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpClientCodec in project rest.li by linkedin.
the class RAPStreamClientPipelineInitializer method initChannel.
@Override
protected void initChannel(NioSocketChannel ch) {
if (_sslContext != null) {
ch.pipeline().addLast(SessionResumptionSslHandler.PIPELINE_SESSION_RESUMPTION_HANDLER, new SessionResumptionSslHandler(_sslContext, _sslParameters, _enableSSLSessionResumption, _sslHandShakeTimeout));
}
ch.pipeline().addLast("codec", new HttpClientCodec(4096, _maxHeaderSize, _maxChunkSize));
ch.pipeline().addLast("rapFullRequestEncoder", new RAPStreamFullRequestEncoder());
ch.pipeline().addLast("rapEncoder", new RAPStreamRequestEncoder());
ch.pipeline().addLast("rapDecoder", new RAPStreamResponseDecoder(_maxResponseSize));
// the response handler catches the exceptions thrown by other layers. By consequence no handlers that throw exceptions
// should be after this one, otherwise the exception won't be caught and managed by R2
ch.pipeline().addLast("responseHandler", new RAPStreamResponseHandler());
ch.pipeline().addLast("channelManager", new ChannelPoolStreamHandler());
}
Aggregations