1
我用netty寫了一個簡單的聊天服務器,朋友,我一直在用telnet進行測試。當我們倆都連接時,它說組的大小是1.每當寫入用戶列表時,它只寫入發送該消息的人。我怎樣才能解決這個問題?Chatty Server with Netty
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
public class ServerChannelHandler extends SimpleChannelHandler {
private ChannelGroup users = new DefaultChannelGroup();
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
users.write(e.getMessage());
}
@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
users.add(e.getChannel());
System.out.println("Opened. ");
System.out.println(users.size());
}
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
users.remove(e.getChannel());
}
}
尼斯,好眼睛諾曼。 +1 –
哇,我是如何錯過的......謝謝你,當一個頻道連接時,我正在創建一個新的ChannelHandler。 – Nick