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; package io.netty.example.securechat;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter; import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.ChannelGroup;
@ -37,19 +39,23 @@ public class SecureChatServerHandler extends ChannelInboundMessageHandlerAdapter
static final ChannelGroup channels = new DefaultChannelGroup(); static final ChannelGroup channels = new DefaultChannelGroup();
@Override @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelActive(final ChannelHandlerContext ctx) throws Exception {
// Once session is secured, send a greeting. // Once session is secured, send a greeting and register the channel to the global channel
ctx.write( // list so the channel received the messages from others.
"Welcome to " + InetAddress.getLocalHost().getHostName() + ctx.pipeline().get(SslHandler.class).handshake().addListener(new ChannelFutureListener() {
" secure chat service!\n"); @Override
ctx.write( public void operationComplete(ChannelFuture future) throws Exception {
"Your session is protected by " + ctx.write(
ctx.pipeline().get(SslHandler.class).getEngine().getSession().getCipherSuite() + "Welcome to " + InetAddress.getLocalHost().getHostName() +
" cipher suite.\n"); " secure chat service!\n");
ctx.write(
"Your session is protected by " +
ctx.pipeline().get(SslHandler.class).getEngine().getSession().getCipherSuite() +
" cipher suite.\n");
// Register the channel to the global channel list channels.add(ctx.channel());
// so the channel received the messages from others. }
channels.add(ctx.channel()); });
} }
@Override @Override