基于Netty实现的Android 消息推送(即时通信)的解决方案

本文档介绍了如何利用Netty框架在Android中构建一个即时通信系统。主要内容包括TCP长连接的建立,心跳机制,消息队列处理,线程池,以及基于Google ProtoBuf的消息协议。服务器端代码展示了NettyServer类的主要组件,如EventLoopGroup、ServerBootstrap、ChannelInitializer,以及消息分发的MessageManager。客户端部分展示了NettyClient的连接、发送消息和心跳处理。通过这样的设计,实现了客户端与服务器之间的消息推送功能。

根据Netty框架实现消息推送(即时聊天)功能.

Netty框架,TCP长连接,心跳,阻塞消息队列,线程池处理消息发送, 基于Google ProtoBuf自定义的消息协议, TCP粘包/拆包....

客户端通过TCP连接到服务器,并建立TCP长连接;当服务器端收到新消息后通过TCP连接推送给客户端, 即消息传递方式: 客户端A -> 服务器 -> 客户端B.

不说了,直接上核心代码吧:


================ Server端核心代码 ===========================

//服务端主类.

public final class NettyServer {
public  static int PORT = 10000;
public  final int HEART_SYNC_TIME = 300;
    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;
    
    public  void bind(int port) throws InterruptedException{
        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap=new ServerBootstrap();
        try {
            bootstrap.group(bossGroup, workerGroup);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
            bootstrap.option(ChannelOption.TCP_NODELAY, true);
            bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    ChannelPipeline pipe = socketChannel.pipeline();
                    pipe.addLast( new ProtobufVarint32FrameDecoder());
                    pipe.addLast(new ProtobufDecoder(MessageProto.Message .getDefaultInstance()));
                    pipe.addLast(new ProtobufVarint32LengthFieldPrepender());
                    pipe.addLast(new ProtobufEncoder());
                    pipe.addLast(new MessageServerHandler());
                }
            });
            // Bind and start to accept incoming connections.
            ChannelFuture f  =  bootstrap.bind(port).sync();


            if(f.isSuccess()){
            Log.debug("server start success... port: "  + port + ", main work thread: " + Thread.currentThread().getId());
            }


            // Wait until the server socket is closed.
            f.channel().closeFuture().sync();
            
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
    
    public synchronized void stop(){
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
    
    public static void main(String []args)  {
    NettyServer server = null;
    try {
    server = new NettyServer();
    if(args != null && args.length>0 && args[0].length() > 3){
    PORT = Integer.parseInt(args[0]);
    }
   
    //message work thread.
    new Thread(new Runnable(){
@Override
public void run() {
MessageManager.getInstance().start();
}
    }).start();
   
    server.bind(PORT);
} catch (InterruptedException e) {
MessageManager.getInstance().stop();
UserManager.getInstance().clearAll();
server.stop();
e.printStackTrace();
}
    }
}


//消息分发

public class MessageManager {
private static MessageManager manager;
private LinkedBlockingQueue<Message> mMessageQueue = new LinkedBlockingQueue<Message>();
private ThreadPoolExecutor mPoolExecutor = new ThreadPoolExecutor(5, 10, 15, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.AbortPolicy());

private MessageManager(){
}

public static MessageManager getInstance(){
if(manager  == null){

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值