http://note.youdao.com/noteshare?id=7c7ddfb6eee6f0d6b32ca28ef3aed77a
http://note.youdao.com/noteshare?id=39ac3922e8db92b12460f55fab034e80
1.简单实例
package com.abc.server;
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 SomeServer {
public static void main(String[] args) throws InterruptedException {
// 用于处理客户端连接请求,将请求发送给childGroup中的eventLoop
EventLoopGroup parentGroup = new NioEventLoopGroup();
// 用于处理客户端请求
EventLoopGroup childGroup = new NioEventLoopGroup();
try {
// 用户启动ServerChannel
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(parentGroup, childGroup) // 指定eventLoopGroup
.channel(NioServerSocketChannel.class) // 指定使用NIO进行通信
.childHandler(new SomeChannelInitializer()); // 指定childGroup中的eventLoop所绑定的线程所要处理的处理器
// 指定当前服务器所监听的端口号
// bind()方法的执行是异步的
// sync()方法会使bind()操作与后续的代码的执行由异步变为了同步
ChannelFuture future = bootstrap.bind(8888).sync();
System.out.println("服务器启动成功。监听的端口号为:8888");
// 关闭Channel
// closeFuture()的执行是异步的。
// 当Channel调用了close()方法并关闭成功后才会触发closeFuture()方法的执行
future.channel().closeFuture().sync();
} finally {
// 优雅关闭
parentGroup.shutdownGracefully();
childGroup.shutdownGracefully();
}
}
}
package com.abc.server;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
// 管道初始化器
// 当前类的实例在pipeline初始化完毕后就会被GC
public class SomeChannelInitializer extends ChannelInitializer {
// 当Channel初始创建完毕后就会触发该方法的执行,用于初始化Channel
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// 从Channel中获取pipeline
ChannelPipeline pipeline = ch.pipeline();
// 将HttpServerCodec处理器放入到pipeline的最后
// HttpServerCodec是什么?是HttpRequestDecoder与HttpResponseEncoder的复合体
// HttpRequestDecoder:http请求解码器,将Channel中的ByteBuf数据解码为HttpRequest对象
// HttpResponseEncoder:http响应编码器,将HttpResponse对象编码为将要在Channel中发送的ByteBuf数据
pipeline.addLast(new HttpServerCodec());
// 将自再定义的处理器放入到Pipeline的最后
pipeline.addLast(new SomeServerHandler());
}
}
package com.abc.server;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import javax.xml.crypto.Data;
// 自定义服务端处理器
// 需求:用户提交一个请求后,在浏览器上就会看到hello netty world
public class SomeServerHandler extends ChannelInboundHandlerAdapter {
/**
* 当Channel中有来自于客户端的数据时就会触发该方法的执行
* @param ctx 上下文对象
* @param msg 就是来自于客户端的数据
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// System.out.println("msg = " + msg.getClass());
// System.out.println("客户端地址 = " + ctx.channel().remoteAddress());
if(msg instanceof HttpRequest) {
HttpRequest request = (HttpRequest) msg;
System.out.println("请求方式:" + request.method().name());
System.out.println("请求URI:" + request.uri());
if("/favicon.ico".equals(request.uri())) {
System.out.println("不处理/favicon.ico请求");
return;
}
// 构造response的响应体
ByteBuf body = Unpooled.copiedBuffer("hello netty world", CharsetUtil.UTF_8);
// 生成响应对象
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, body);
// 获取到response的头部后进行初始化
HttpHeaders headers = response.headers();
headers.set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
headers.set(HttpHeaderNames.CONTENT_LENGTH, body.readableBytes());
// 将响应对象写入到Channel
// ctx.write(response);
// ctx.flush();
// ctx.writeAndFlush(response);
ctx.writeAndFlush(response)
// 添加监听器,响应体发送完毕则直接将Channel关闭
.addListener(ChannelFutureListener.CLOSE);
}
}
/**
* 当Channel中的数据在处理过程中出现异常时会触发该方法的执行
* @param ctx 上下文
* @param cause 发生的异常对象
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
// 关闭Channel
ctx.close();
}
}
2.网络聊天室
package com.abc.server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class SomeServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup parentGroup = new NioEventLoopGroup();
EventLoopGroup childGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(parentGroup, childGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// 添加一个基于行的解码器
pipeline.addLast(new LineBasedFrameDecoder(2048));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new SomeServerHandler());
}
});
ChannelFuture future = bootstrap.bind(8888).sync();
System.out.println("服务器已启动");
future.channel().closeFuture().sync();
} finally {
parentGroup.shutdownGracefully();
childGroup.shutdownGracefully();
}
}
}
package com.abc.server;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class SomeServerHandler extends ChannelInboundHandlerAdapter {
// 创建一个ChannelGroup,其是一个线程安全的集合,其中存放着与当前服务器相连接的所有Active状态的Channel
// GlobalEventExecutor是一个单例、单线程的EventExecutor,是为了保证对当前group中的所有Channel的处理
// 线程是同一个线程
private static ChannelGroup group = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
// 只要有客户端Channel给当前的服务端发送了消息,那么就会触发该方法的执行
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 获取到向服务器发送消息的channel
Channel channel = ctx.channel();
// 这里要实现将消息广播给所有group中的客户端Channel
// 发送给自己的消息与发送给大家的消息是不一样的
group.forEach(ch -> {
if(ch != channel) {
ch.writeAndFlush(channel.remoteAddress() + ":" + msg + "\n");
} else {
channel.writeAndFlush("me:" + msg + "\n");
}
});
}
// 只要有客户端Channel与服务端连接成功就会执行这个方法
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// 获取到当前与服务器连接成功的channel
Channel channel = ctx.channel();
System.out.println(channel.remoteAddress() + "---上线");
group.writeAndFlush(channel.remoteAddress() + "---上线\n");
// 将当前channel添加到group中
group.add(channel);
}
// 只要有客户端Channel断开与服务端的连接就会执行这个方法
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// 获取到当前要断开连接的Channel
Channel channel = ctx.channel();
System.out.println(channel.remoteAddress() + "------下线");
group.writeAndFlush(channel.remoteAddress() + "下线,当前在线人数:" + group.size() + "\n");
// group中存放的都是Active状态的Channel,一旦某Channel的状态不再是Active,
// group会自动将其从集合中踢出,所以,下面的语句不用写
// remove()方法的应用场景是,将一个Active状态的channel移出group时使用
// group.remove(channel);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
package com.abc.client;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class SomeClient {
public static void main(String[] args) throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LineBasedFrameDecoder(2048));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new SomeClientHandler());
}
});
ChannelFuture future = bootstrap.connect("localhost", 8888).sync();
// 获取键盘输入
InputStreamReader is = new InputStreamReader(System.in, "UTF-8");
BufferedReader br = new BufferedReader(is);
// 将输入的内容写入到Channel
future.channel().writeAndFlush(br.readLine() + "\r\n");
}
}
package com.abc.client;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
public class SomeClientHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}