Netty是Apache团队的又一个优秀的Socket框架,它和mina是一个团队开发的,所以很多思想是相同的,接下来,我们就来实现客户端和服务端的双向通信。
/**
* 消息类型
* @author 李熠
*
*/
public enum MsgType {
}
分别是心跳、发送、登录、找不到目标
当客户端和服务端连接后,需要向服务端发送登录请求,也就是消息类型:LOGIN,服务端接收到LOGIN请求后,会将客户端加入到队列中,
import java.io.Serializable;
public class Message implements Serializable {
}
这类是定义的消息Bean,想服务端发送消息就是发送的这个对象的数据。
接下来,实现客户端队列代码:
import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class NettyChannelMap {
}
服务端:
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
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.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.nio.charset.Charset;
public class NettyServer {
}
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import cn.sunsharp.netty.bean.Message;
import cn.sunsharp.netty.bean.MsgType;
import cn.sunsharp.netty.bean.NettyChannelMap;
import com.alibaba.fastjson.JSON;
//最好继承
SimpleChannelInboundHandler表示传递字符串消息,handler会把json格式的字符串转换为Message对象
public class NettyServerHandler extends SimpleChannelInboundHandler{@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { //channel失效,从Map中移除 NettyChannelMap.remove((SocketChannel)ctx.channel()); }@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)throws Exception {//cause.printStackTrace();System.out.println(“出现异常!”);}@Overrideprotected void messageReceived(ChannelHandlerContext ctx, String msg)throws Exception {System.out.println(msg);Message message = JSON.parseObject(msg+”“, Message.class);System.out.println(“接收到消息:”+message);String clientId = message.getClientId();if(MsgType.LOGIN.equals(message.getType())){System.out.printf(“将%s添加到队列\n”,clientId); NettyChannelMap.add(clientId,(SocketChannel)ctx.channel()); }else{ if(NettyChannelMap.get(clientId)==null){ System.out.printf(“登录失败,请重新登录!”,clientId); //说明未登录,或者连接断了,服务器向客户端发起登录请求,让客户端重新登录 message = new Message(MsgType.LOGIN); ctx.channel().writeAndFlush(JSON.toJSONString(message)); } } switch (message.getType()){ case PING:{ message = new Message(MsgType.PING); NettyChannelMap.get(clientId).writeAndFlush(JSON.toJSONString(message)); }break; case SEND:{ //收到客户端的请求,发送给targetId System.out.println(“发送消息:”+message); if(NettyChannelMap.get(message.getTargetId()) != null){ NettyChannelMap.get(message.getTargetId()).writeAndFlush(JSON.toJSONString(message)); }else{ message.setType(MsgType.NO_TARGET); NettyChannelMap.get(clientId).writeAndFlush(JSON.toJSONString(message)); } }break; default:break; }}}客户端可以使用任何框架任何语言的Socket来连接并发送消息,为了方便,这里依然用Netty来实现客户端:
import java.nio.charset.Charset;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import cn.sunsharp.regulation.bean.Message;
import cn.sunsharp.regulation.bean.MsgType;
import com.alibaba.fastjson.JSON;
public class NettyClient {
// if(null != future){
// if(null != future.channel() && future.channel().isOpen()){
// future.channel().close();
// }
// }
// System.out.println(“准备重连!”);
// start();
}
}
public static void main(String[]args) throws InterruptedException {
NettyClient bootstrap=new NettyClient(9999,”192.168.1.38”);
System.out.println(11111);
Message loginMsg=new Message(MsgType.LOGIN);
loginMsg.setClientId(“001”);
loginMsg.setTargetId(“192.168.1.38”);
loginMsg.setType(MsgType.LOGIN);
bootstrap.socketChannel.writeAndFlush(JSON.toJSON(loginMsg));
}
}
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import cn.sunsharp.regulation.bean.Message;
import cn.sunsharp.regulation.bean.MsgType;
import com.alibaba.fastjson.JSON;
public class NettyClientHandler extends SimpleChannelInboundHandler {
}