目錄
需求功能
客戶端:
- 連接服務器,可以從鍵盤輸入讀取數據發送給服務器端
- 接收服務端的發來的消息
服務器端:
- 可以監測客戶端(用戶)上線、離線
- 實現消息轉發給其它客戶端(用戶)
- 向其它客戶端(用戶)提示某個客戶端的加入和離開
實現思路
客戶端:
- 首先用戶發送和接收的是字符串,在pipeline加入字符串解碼器(StringDecoder)和字符串編碼器(StringEncoder)
- 與服務端建立連接後,使用Scanner從鍵盤循環讀取數據,然後向連接後的Channel中發送數據
- 爲了方便業務處理器選擇SimpleChannelInboundHandler,能夠直接把解碼器得到的字符串以參數的形式直接傳入到回調方法中
服務器端:
- 同客戶端一樣,在pipeline加入字符串解碼器(StringDecoder)和字符串編碼器(StringEncoder)
- 同客戶端一樣選擇SimpleChannelInboundHandler做爲業務處理器,使用ChannelGroup(集合)保存所有客戶端
- channelActive方法表示channel處於活動狀態,提示xxx上線了
- channelInactive方法表示channel處於不活動狀態,提示xxx下線了
- handlerAdded方法表示連接建立,當客戶端上來一旦連接時被執行,在此方法中向channel集合所有用戶通知xxx加入聊天室
- handlerRemoved方法表示斷開連接,當客戶端與服務器斷開連接時被執行,在此方法中向channel集合有用戶通知xxx離開聊天室
服務端實現
服務器實現類
public class NettyGroupChatServer {
private int port;
public NettyGroupChatServer(int port){
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGoup = new NioEventLoopGroup(8);
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGoup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// bytebuffer->String
pipeline.addLast("decoder", new StringDecoder());
// string->bytebuffer
pipeline.addLast("encoder", new StringEncoder());
// 業務處理器
pipeline.addLast(new GroupChatServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
channelFuture.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGoup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new NettyGroupChatServer(7777).run();
}
}
業務處理器:
public class GroupChatServerHandler extends SimpleChannelInboundHandler<String> {
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 將當前 channel 加入到 channelGroup
* @param ctx
* @throws Exception
*/
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
channelGroup.writeAndFlush("【客戶端】 " + channel.remoteAddress() + " 加入了聊天" + sdf.format(new Date()) + "\n");
channelGroup.add(channel);
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
channelGroup.writeAndFlush("【客戶端】" + channel.remoteAddress() + " 離開了\n" );
System.out.println("channelGroup size" + channelGroup.size());
}
// 表示 channel 處於活動狀態, 提示 xx 上線
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().remoteAddress() + " 上線了~");
}
// 表示 channel 處於不活動狀態, 提示 xx 離線了
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().remoteAddress() + " 離線了~");
}
/**
* 讀取數據
* @param ctx
* @param msg
* @throws Exception
*/
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
// 獲取當時channel,把信息轉發到除channelGroup中的其它chanel上
Channel channel = ctx.channel();
channelGroup.forEach(ch ->{
if (channel != ch){
ch.writeAndFlush("[客戶] " + channel.remoteAddress() + " 發送了消息" + msg + "\n");
}else{
ch.writeAndFlush("[自已]發送了消息" + msg + "\n");
}
});
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
客戶端實現
客戶端啓動類:
public class NettyGroupChatClient {
private final String host;
private final int port;
public NettyGroupChatClient(String host, int port) {
this.host = host;
this.port = port;
}
public void run() throws InterruptedException {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try{
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// bytebuffer->String
pipeline.addLast("decoder", new StringDecoder());
// string->bytebuffer
pipeline.addLast("encoder", new StringEncoder());
// 業務處理器
pipeline.addLast(new GroupChatClientHandler());
}
});
// 連接服務端
ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
// 獲取通道
Channel channel = channelFuture.channel();
// 發送數據
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){
String content = scanner.nextLine();
channel.writeAndFlush(content + "\r\n");
}
}finally {
eventLoopGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws InterruptedException {
new NettyGroupChatClient("127.0.0.1", 7777).run();
}
}
客戶端業務處理器:
public class GroupChatClientHandler extends SimpleChannelInboundHandler<String> {
/**
* 讀取數據
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(msg.trim());
}
}