Fix a bug in the SecureChat example where greeting is generated too soon

This commit is contained in:
Trustin Lee 2012-10-16 15:00:32 -07:00
parent a6c4f651a7
commit 8bab0aae9e

View File

@ -16,6 +16,8 @@
package io.netty.example.securechat;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.group.ChannelGroup;
@ -37,8 +39,12 @@ public class SecureChatServerHandler extends ChannelInboundMessageHandlerAdapter
static final ChannelGroup channels = new DefaultChannelGroup();
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// Once session is secured, send a greeting.
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
// Once session is secured, send a greeting and register the channel to the global channel
// list so the channel received the messages from others.
ctx.pipeline().get(SslHandler.class).handshake().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
ctx.write(
"Welcome to " + InetAddress.getLocalHost().getHostName() +
" secure chat service!\n");
@ -47,10 +53,10 @@ public class SecureChatServerHandler extends ChannelInboundMessageHandlerAdapter
ctx.pipeline().get(SslHandler.class).getEngine().getSession().getCipherSuite() +
" cipher suite.\n");
// Register the channel to the global channel list
// so the channel received the messages from others.
channels.add(ctx.channel());
}
});
}
@Override
public void messageReceived(ChannelHandlerContext ctx, String request) throws Exception {