我做了一段时间的网络编程,最近把我的一个项目转换成了netty。我很担心,与我的原始程序不同,客户端在关闭前会冻结大约3-5秒,这足以让我每次都被迫终止它,因为我不想等待它。这对netty来说是正常的吗?我做错什么了吗?
主要方法:
public static void main(String[] args) throws Exception {
final SslContext sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new NetworkInitializer(sslCtx));
// Start the connection attempt.
ch = b.connect(HOST, PORT).sync().channel();
//sends the clients name to the server
askname();
//loop just does some simple gl stuff and gameplay updating, posted below
while(running){loop();
if (Display.isCloseRequested()){
running = false;
if (lastWriteFuture != null) {
lastWriteFuture.sync();
} //tells the server to shut down
write("9");
ch.closeFuture().sync();
group.shutdownGracefully();
System.out.println("herro der");
break;
}
}
} finally {
group.shutdownGracefully();
// The connection is closed automatically on shutdown.
}
}
循环类:
private static void loop() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
//main update method for my game. does calculations and stuff. I can post it if neccisary but its kinda big.
SpaceState.update();
Display.update();
Display.sync(60);
}