本篇采用的编解码是netty自带的字符串的格式的编解码,用户1可以控制台发消息(输入完消息要加换行符号,是一个消息结束的分隔符,后面我会在编解码中详细讲解)给服务器,服务器广播给其他所有的客户端,这个机制,我们游戏开发服务器中应用很普遍,游戏中的广播,游戏中你看到其他玩家在操作。。。都是利用这个广播机制。
案例如下:
package com.zhurong.netty.test3;
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;
/**
* Description:
* User: zhurong
* Date: 2018-09-24 23:17
*/
public class NettyChatServer {
public static void main(String[] args) {
//接收连接
EventLoopGroup bossGroup = new NioEventLoopGroup();
//连接发送给work
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
System.out.println("服务器启动成功!");
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).
childHandler(new NettyChatServerInitializer());
ChannelFuture channelFuture = serverBootstrap.bind(8000).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
package com.zhurong.netty.test3;
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;
import io.netty.util.CharsetUtil;
/**
* Description: 客户端与服务器端连接一旦创建,这个类中方法就会被回调
* User: zhurong
* Date: 2018-09-24 21:29
*/
public class NettyChatServerInitializer extends ChannelInitializer {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//解码器
pipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new NettyChatServerHandler());
}
}
package com.zhurong.netty.test3;
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;
/**
* 此处定义客户端和服务器端传递的是字符串所以用了String
* Description:
* User: zhurong
* Date: 2018-09-24 23:30
*/
public class NettyChatServerHandler extends SimpleChannelInboundHandler {
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(ctx.channel().remoteAddress()+"------>"+msg);
//返回数据给客户端,是一个异步的操作
channelGroup.forEach(channel ->{
System.out.println("channel:"+channel);
if(ctx.channel() != channel){
channel.writeAndFlush(ctx.channel().remoteAddress()+"发送的消息" + msg+"\n");
System.out.println("发送消息给客户端:"+ctx.channel().remoteAddress()+",msg:"+msg+"\n");
}else {
channel.writeAndFlush("是自己的消息\n");
System.out.println("是自己的消息:"+ msg);
}
});
// channelGroup.writeAndFlush("服务器已经收到你们客户端消息了!");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
System.out.println("server 推送的消息:"+ ctx.channel().remoteAddress() + "注册进了服务器\n");
channelGroup.writeAndFlush("server 推送的消息:"+ ctx.channel().remoteAddress() + "注册进了服务器\n");
channelGroup.add(ctx.channel());
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
channelGroup.writeAndFlush("server 推送的消息:"+ ctx.channel().remoteAddress() + "离开了服务器\n");
System.out.println("server 推送的消息:"+ ctx.channel().remoteAddress() + "离开了服务器\n");
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
channelGroup.writeAndFlush(ctx.channel().remoteAddress() + "上线了\n");
System.out.println(ctx.channel().remoteAddress() + "上线了\n");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
channelGroup.writeAndFlush(ctx.channel().remoteAddress() + "下线了\n");
System.out.println(ctx.channel().remoteAddress() + "下线了\n");
}
}
package com.zhurong.netty.test3;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Description:
* User: zhurong
* Date: 2018-09-24 23:37
*/
public class NettyChatClient {
public static void main(String[] args) throws InterruptedException, IOException {
EventLoopGroup eventExecutors = new NioEventLoopGroup();
try {
System.out.println("客户端启动成功");
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventExecutors).channel(NioSocketChannel.class).handler(new NettyChatClientInitializer());
ChannelFuture channelFuture = bootstrap.connect("localhost",8000).sync();
Channel channel = channelFuture.channel();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
for(;;){
channel.writeAndFlush(bufferedReader.readLine() + "\r\n");
}
// channelFuture.channel().closeFuture().sync();
}finally {
eventExecutors.shutdownGracefully();
}
}
}
package com.zhurong.netty.test3;
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.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;
/**
* Netstat –ano|findstr
* Description: 客户端与服务器端连接一旦创建,这个类中方法就会被回调
* User: zhurong
* Date: 2018-09-24 21:29
*/
public class NettyChatClientInitializer extends ChannelInitializer {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//解码器
pipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new NettyChatClientHandler());
}
}
package com.zhurong.netty.test3;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
/**
* Description:
* User: zhurong
* Date: 2018-09-24 22:01
*/
public class NettyChatClientHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("收到服务器的消息:"+ctx.channel().remoteAddress() + msg);
// System.out.println("msg:"+ msg);
// ctx.writeAndFlush("from client "+ System.currentTimeMillis());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush("acitve客户端第一条信息");
}
}
现在是不是对netty又进步一熟悉了!