Simplified HTTP tunneling example
This commit is contained in:
parent
43d2fb47bd
commit
cf57e4c42b
@ -64,25 +64,26 @@ public class HttpTunnelingClientExample {
|
|||||||
URI uri = new URI(args[0]);
|
URI uri = new URI(args[0]);
|
||||||
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
||||||
|
|
||||||
HttpTunnelingClientSocketChannelFactory factory = new HttpTunnelingClientSocketChannelFactory(new OioClientSocketChannelFactory(Executors.newCachedThreadPool()));
|
ClientBootstrap b = new ClientBootstrap(
|
||||||
ClientBootstrap bootstrap = new ClientBootstrap(factory);
|
new HttpTunnelingClientSocketChannelFactory(
|
||||||
bootstrap.getPipeline().addLast("decoder", new StringDecoder());
|
new OioClientSocketChannelFactory(Executors.newCachedThreadPool())));
|
||||||
bootstrap.getPipeline().addLast("encoder", new StringEncoder());
|
b.getPipeline().addLast("decoder", new StringDecoder());
|
||||||
bootstrap.getPipeline().addLast("handler", new LoggingHandler(InternalLogLevel.INFO));
|
b.getPipeline().addLast("encoder", new StringEncoder());
|
||||||
|
b.getPipeline().addLast("handler", new LoggingHandler(InternalLogLevel.INFO));
|
||||||
|
|
||||||
bootstrap.setOption("serverName", uri.getHost());
|
b.setOption("serverName", uri.getHost());
|
||||||
bootstrap.setOption("serverPath", uri.getRawPath());
|
b.setOption("serverPath", uri.getRawPath());
|
||||||
|
|
||||||
// Configure SSL if necessary
|
// Configure SSL if necessary
|
||||||
if (scheme.equals("https")) {
|
if (scheme.equals("https")) {
|
||||||
bootstrap.setOption("sslContext", SecureChatSslContextFactory.getClientContext());
|
b.setOption("sslContext", SecureChatSslContextFactory.getClientContext());
|
||||||
} else if (!scheme.equals("http")) {
|
} else if (!scheme.equals("http")) {
|
||||||
// Only HTTP and HTTPS are supported.
|
// Only HTTP and HTTPS are supported.
|
||||||
System.err.println("Only HTTP(S) is supported.");
|
System.err.println("Only HTTP(S) is supported.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(uri.getHost(), uri.getPort()));
|
ChannelFuture channelFuture = b.connect(new InetSocketAddress(uri.getHost(), uri.getPort()));
|
||||||
channelFuture.awaitUninterruptibly();
|
channelFuture.awaitUninterruptibly();
|
||||||
System.out.println("Enter text ('quit' to exit)");
|
System.out.println("Enter text ('quit' to exit)");
|
||||||
// Read commands from the stdin.
|
// Read commands from the stdin.
|
||||||
@ -107,6 +108,6 @@ public class HttpTunnelingClientExample {
|
|||||||
// Wait until the connection is closed or the connection attempt fails.
|
// Wait until the connection is closed or the connection attempt fails.
|
||||||
channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
|
channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
|
||||||
|
|
||||||
factory.releaseExternalResources();
|
b.releaseExternalResources();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user