Git Product home page Git Product logo

android-tcp-long-connection-based-on-apache-mina's Introduction

TCP-long-connection-based-on-Apache-mina

基于Apache mina 的tcp长连接实现,可用于android客户端推送。

项目将Apache的mina项目移植到了android平台。实现长连接的主要**是使用了mina的KeepAliveFilter过滤器。

acceptor.getFilterChain().addLast("keeplive", new KeepAliveFilter(new ServerKeepAliveMessageFactoryImp(), IdleStatus.READER_IDLE, KeepAliveRequestTimeoutHandler.CLOSE,10, 5));

Android客户端:

核心代码如下图所示,规定了长连接ping与pong信息的规则,以及网络参数配置信息。

配置信息:

/**
 * 服务器地址
 */
public static final String HOSTNAME = "192.168.1.15";
/**
 * 服务器端口号
 */
public static final int PORT = 8081;
/**
 * 连接超时时间,30 seconds
 */
public static final long SOCKET_CONNECT_TIMEOUT = 30 * 1000L;
/**
 * 长连接心跳包发送频率,10 seconds
 */
public static final int KEEP_ALIVE_TIME_INTERVAL = 10;
/**
 * 长连接心跳包应答超时
 */
public static final int KEEP_ALIVE_RESPONSE_TIMEOUT = 5;
/**
 * 心跳包 ping message
 */
public static final String PING_MESSAGE="ping";
/**
 * 心跳包 pong message
 */
public static final String PONG_MESSAGE="pong";

程序启动后,PushService启动,并开始与服务器连接。

服务器端核心代码:

服务器与客户端的ping与pong信息要保持一致。服务器启动LongTcpServer即可。

在服务器可以看到日志信息:

可以看到,成功建立了链接,并且每隔10秒都会受到ping信息,并发送pong信息应答。

android-tcp-long-connection-based-on-apache-mina's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.