Netty网络编程实战之开发聊天室功能
在 Netty网络编程实战1,搭建第一个Netty服务器中,使用curl作为客户端访问,下面将通过Netty实现客户端,客户端代码依然采用Netty老套路 主程序类+自定义初始化器+自定义处理器三部分组成。
一、服务端
1、主程序类
package com.guor.demo.netty.chat;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.NIO.NioEventLoopGroup;
import io.netty.channel.Socket.nio.NiOServerSocketChannel;
public class MyNettyServerTest {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup serverGroup = new NioEventLoopGroup();
EventLoopGroup clientGroup = new NioEventLoopGroup();
try {
// 服务端启动时的初始化操作
ServerBootstrap serverBootstrap = new ServerBootstrap();
// 1、将serverGroup和clientGroup注册到服务端的Channel上;
// 2、注册一个服务端的初始化器MyNettyServerInitializer;
// 3、该初始化器中的initChannel()方法会在连接被注册到Channel后立刻执行;
// 5、最后将端口号绑定到8080;
ChannelFuture channelFuture = serverBootstrap.group(serverGroup, clientGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new MyNettyServerInitializer()).bind(8080).sync();
channelFuture.channel().closeFuture().sync();
}catch (Exception e){
System.out.println(e);
}finally {
serverGroup.shutdownGracefully();
clientGroup.shutdownGracefully();
}
}
}
2、自定义初始化器
package com.guor.demo.netty.chat;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
public class MyNettyServerInitializer extends ChannelInitializer<SocketChannel> {
// 连接被注册到Channel后,立刻执行此方法
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("LengthFieldBasedFrameDecoder",new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,8,0,8));
// 将上条语句的length加入传递的数据中心
pipeline.addLast("LengthFieldPrepender",new LengthFieldPrepender(8));
// 传递字符串的编码解码器
pipeline.addLast("StringDecoder",new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("StringEecoder",new StringEncoder(CharsetUtil.UTF_8));
// 增加自定义处理器MyNettyServerHandler,用于实际处理请求,并给出响应
pipeline.addLast("MyNettyServerHandler",new MyNettyServerHandler());
}
}
3、自定义处理器
package com.guor.demo.netty.chat;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.Scanner;
public class MyNettyServerHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String receiveMsg) throws Exception {
// 通过ctx获取客户端的IP和端口号,并打印出客户端发来的消息
System.out.println("服务端接收的请求来自:"+ctx.channel().remoteAddress()+",消息内容:"+receiveMsg);
System.out.println("请向客户端发送一条消息:");
String sendMsg = new Scanner(System.in).nextLine();
ctx.channel().writeAndFlush(sendMsg);
}
}
二、客户端
1、主程序类
package com.guor.demo.netty.chat;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
public class MyNettyClientTest {
public static void main(String[] args) {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
// 客户端启动时的初始化操作
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.handler(new MyNettyClientInitializer());
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync();
channelFuture.channel().closeFuture().sync();
}catch (Exception e){
System.out.println(e);
}finally {
eventLoopGroup.shutdownGracefully();
}
}
}
2、自定义初始化器
package com.guor.demo.netty.chat;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
public class MyNettyClientInitializer extends ChannelInitializer<SocketChannel> {
// 连接被注册后,立即执行此方法
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("LengthFieldBasedFrameDecoder",new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,8,0,8));
// 将上条语句的length加入传递的数据中心
pipeline.addLast("LengthFieldPrepender",new LengthFieldPrepender(8));
// 传递字符串的编码解码器
pipeline.addLast("StringDecoder",new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("StringEecoder",new StringEncoder(CharsetUtil.UTF_8));
// 增加自定义处理器MyNettyClientHandler
pipeline.addLast("MyNettyClientHandler",new MyNettyClientHandler());
}
}
3、自定义处理器
package com.guor.demo.netty.chat;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.Scanner;
public class MyNettyClientHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String receiveMsg) throws Exception {
System.out.println("客户端接收的请求来自:"+ctx.channel().remoteAddress()+",消息内容:"+receiveMsg);
System.out.println("请向服务端发送一条消息:");
String sendMsg = new Scanner(System.in).nextLine();
ctx.channel().writeAndFlush(sendMsg);
}
public void channelActive(ChannelHandlerContext ctx) throws Exception{
ctx.writeAndFlush("第一条消息...");
}
}
三、启动服务端、客户端
1、服务端:你好,我是服务端,哪吒编程
2、客户端:我去,还真连上了,第一次使用Netty通话,真神奇
3、服务端:土包子
到此这篇关于Netty网络编程实战之开发聊天室功能的文章就介绍到这了,更多相关Netty聊天室内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章