参看Netty入门(一):Netty入门(一)之webSocket聊天室
Netty4.X下载地址:http://netty.io/downloads.htmlhtml
1.SimpleChatServerHandler.javajava
package cn.zyzpp.netty4.service;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
/** * Created by 巅峰小学生 * 2018年3月4日 */
public class SimpleChatServerHandler extends SimpleChannelInboundHandler<String> { // (1)
public static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
/** * 每当从服务端收到新的客户端链接时,客户端的 Channel 存入ChannelGroup列表中, * 并通知列表中的其余客户端 Channel */
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception { // (2)
Channel incoming = ctx.channel();
for (Channel channel : channels) {
channel.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " 加入\n");
}
channels.add(ctx.channel());
}
/** * 每当从服务端收到客户端断开时,客户端的 Channel 移除 ChannelGroup 列表中, * 并通知列表中的其余客户端 Channel */
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { // (3)
Channel incoming = ctx.channel();
for (Channel channel : channels) {
channel.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " 离开\n");
}
channels.remove(ctx.channel());
}
/** * 每当从服务端读到客户端写入信息时,将信息转发给其余客户端的 Channel。 * 其中若是你使用的是 Netty 5.x 版本时,须要把 channelRead0() 重命名为messageReceived() */
@Override
protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception { // (4)
Channel incoming = ctx.channel();
for (Channel channel : channels) {
if (channel != incoming) {
channel.writeAndFlush("[" + incoming.remoteAddress() + "]" + s + "\n");
} else {
channel.writeAndFlush("[you]" + s + "\n");
}
}
}
/** * 服务端监听到客户端活动 */
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { // (5)
Channel incoming = ctx.channel();
System.out.println("SimpleChatClient:" + incoming.remoteAddress() + "在线");
}
/** * 服务端监听到客户端不活动 */
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { // (6)
Channel incoming = ctx.channel();
System.out.println("SimpleChatClient:" + incoming.remoteAddress() + "掉线");
}
/** * 当出现 Throwable 对象才会被调用 */
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (7)
Channel incoming = ctx.channel();
System.out.println("SimpleChatClient:" + incoming.remoteAddress() + "异常");
// 当出现异常就关闭链接
cause.printStackTrace();
ctx.close();
}
}
- 1.SimpleChatServerHandler 继承自 SimpleChannelInboundHandler,这个类实现了ChannelInboundHa
ndler接口,ChannelInboundHandler 提供了许多事件处理的接口方法,而后你能够覆盖这些方法。如今仅仅
只须要继承 SimpleChannelInboundHandler 类而不是你本身去实现接口方法。
2.覆盖了 handlerAdded() 事件处理方法。每当从服务端收到新的客户端链接时,客户端的 Channel 存入Chan
nelGroup列表中,并通知列表中的其余客户端 Channel
3.覆盖了 handlerRemoved() 事件处理方法。每当从服务端收到客户端断开时,客户端的 Channel 移除 Chan
nelGroup 列表中,并通知列表中的其余客户端 Channel
4.覆盖了 channelRead0() 事件处理方法。每当从服务端读到客户端写入信息时,将信息转发给其余客户端的 C
hannel。其中若是你使用的是 Netty 5.x 版本时,须要把 channelRead0() 重命名为messageReceived()
5.覆盖了 channelActive() 事件处理方法。服务端监听到客户端活动
6.覆盖了 channelInactive() 事件处理方法。服务端监听到客户端不活动
7.exceptionCaught() 事件处理方法是当出现 Throwable 对象才会被调用,即当 Netty 因为 IO 错误或者处理
器在处理事件时抛出的异常时。在大部分状况下,捕获的异常应该被记录下来而且把关联的 channel 给关闭
掉。然而这个方法的处理方式会在遇到不一样异常的状况下有不一样的实现,好比你可能想在关闭链接以前发送一个
错误码的响应消息。
2.SimpleChatServerInitializer.javaweb
- SimpleChatServerInitializer 用来增长多个的处理类到 ChannelPipeline 上,包括编码、解码、SimpleChatS
erverHandler 等。
package cn.zyzpp.netty4.service;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
/** * Created by 巅峰小学生 * 2018年3月4日 * 初始化链接时候的各个组件 */
public class SimpleChatServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new SimpleChatServerHandler());
System.out.println("SimpleChatClient:" + ch.remoteAddress() + "链接上");
}
}
3.SimpleChatServer.javabootstrap
- 编写一个 main() 方法来启动服务端。
package cn.zyzpp.netty4.service;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class SimpleChatServer {
private int port;
public SimpleChatServer(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new SimpleChatServerInitializer()) // (4)
.option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
System.out.println("SimpleChatServer 启动了");
// 绑定端口,开始接收进来的链接
ChannelFuture f = b.bind(port).sync(); // (7)
// 等待服务器 socket 关闭 。
// 在这个例子中,这不会发生,但你能够优雅地关闭你的服务器。
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
System.out.println("SimpleChatServer 关闭了");
}
}
public static void main(String[] args) throws Exception {
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
port = 8888;
}
new SimpleChatServer(port).run();
}
}
- 1.NioEventLoopGroup是用来处理I/O操做的多线程事件循环器,Netty 提供了许多不一样的EventLoopGroup的
实现用来处理不一样的传输。在这个例子中咱们实现了一个服务端的应用,所以会有2个 NioEventLoopGroup 会
被使用。第一个常常被叫作‘boss’,用来接收进来的链接。第二个常常被叫作‘worker’,用来处理已经被
接收的链接,一旦‘boss’接收到链接,就会把链接信息注册到‘worker’上。如何知道多少个线程已经被使
用,如何映射到已经建立的 Channel上都须要依赖于 EventLoopGroup 的实现,而且能够经过构造函数来配置
他们的关系。
2.ServerBootstrap是一个启动 NIO 服务的辅助启动类。你能够在这个服务中直接使用 Channel,可是这会是
一个复杂的处理过程,在不少状况下你并不须要这样作。
3.这里咱们指定使用NioServerSocketChannel类来举例说明一个新的 Channel 如何接收进来的链接。
4.这里的事件处理类常常会被用来处理一个最近的已经接收的 Channel。SimpleChatServerInitializer 继承自C
hannelInitializer是一个特殊的处理类,他的目的是帮助使用者配置一个新的 Channel。也许你想经过增长一些
处理类好比 SimpleChatServerHandler 来配置一个新的 Channel 或者其对应的ChannelPipeline来实现你的
网络程序。当你的程序变的复杂时,可能你会增长更多的处理类到 pipline 上,而后提取这些匿名类到最顶层的类
上。
5.你能够设置这里指定的 Channel 实现的配置参数。咱们正在写一个TCP/IP 的服务端,所以咱们被容许设置 s
ocket 的参数选项好比tcpNoDelay 和 keepAlive。请参考ChannelOption和详细的ChannelConfig实现的接
口文档以此能够对ChannelOption 的有一个大概的认识。
6.option() 是提供给NioServerSocketChannel用来接收进来的链接。childOption() 是提供给由父管道Server
Channel接收到的链接,在这个例子中也是 NioServerSocketChannel。
7.咱们继续,剩下的就是绑定端口而后启动服务。这里咱们在机器上绑定了机器全部网卡上的 8080 端口。固然
如今你能够屡次调用 bind() 方法(基于不一样绑定地址)。
恭喜!你已经完成了基于 Netty 聊天服务端程序。
1.SimpleChatClientHandler.java服务器
- 客户端的处理类比较简单,只须要将读到的信息打印出来便可
package cn.zyzpp.netty4.client;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
/** * Created by 巅峰小学生 * 2018年3月4日 下午5:46:46 */
public class SimpleChatClientHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
System.out.println(s);
}
}
2.SimpleChatClientInitializer.java网络
- 与服务端相似
package cn.zyzpp.netty4.client;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
/** * Created by 巅峰小学生 * 2018年3月4日 下午5:48:08 */
public class SimpleChatClientInitializer extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new SimpleChatClientHandler());
}
}
3.SimpleChatClient.java多线程
- 编写一个 main() 方法来启动客户端。
package cn.zyzpp.netty4.client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
/** * Created by 巅峰小学生 * 2018年3月4日 下午5:50:44 */
public class SimpleChatClient {
public static void main(String[] args) throws Exception {
new SimpleChatClient("localhost", 8080).run();
}
private final String host;
private final int port;
public SimpleChatClient(String host, int port) {
this.host = host;
this.port = port;
}
public void run() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class)
.handler(new SimpleChatClientInitializer());
Channel channel = bootstrap.connect(host, port).sync().channel();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
channel.writeAndFlush(in.readLine() + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}
先运行 SimpleChatServer,再能够运行多个 SimpleChatClient,控制台输入文本继续测试 socket