public final class WebSocketServer {
static final boolean SSL = System.getProperty("ssl") != null;
static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));
public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
} else {
sslCtx = null;
}
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new WebSocketServerInitializer(sslCtx));
//从这里就有点看不懂了,这里绑定了一个端口,这个sync是什么?
//文档上说:等待这个未来直到它完成,如果这个未来失败,重新抛出失败的原因。
//文档上说的感觉就是java中serversocket的acpte接收方法,而且还是同步接收
//但实时指定不是,因为执行完sync这段代码,程序并没有阻塞,而是继续执行下面的代码了
Channel ch = b.bind(PORT).sync().channel();
System.out.println("Open your web browser and navigate to " +
(SSL? "https" : "http") + "://127.0.0.1:" + PORT + '/');
//程序执行到这里开始阻塞,等待有计算机连接进来
ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
对于netty中sync()方法的一些疑问~
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答
- 淡定的攻城狮_LJ 2017-04-15 03:51关注
不一样的,sync和java的accept不一样,你会发现netty中的很多操作都会调用sync来启动,感觉是有点异步操作的意思,而不是同步阻塞
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报