一、说明
客户端和服务器端采用Apache的mina框架实现了APP的长连接
二、JAVA服务端
创建一个基于TCP/IP的非阻塞的server端的Socket编写过滤器(使用mina提供的文本换行符编解码器)设置读取数据的缓冲区大小设置读写通道10秒内无操作进入空闲状态为接收器设置管理服务为Socket绑定端口
2.1长连接服务器端代码
public class Demo1Server {
private static Logger logger = Logger.getLogger(Demo1Server.class+
"");
private static int PORT =
9088;
public static void main(String[] args){
IoAcceptor acceptor =
null;
try{
acceptor =
new NioSocketAcceptor();
acceptor.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(
new TextLineCodecFactory(Charset.forName(
"UTF-8"),LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));
acceptor.getSessionConfig().setReadBufferSize(
2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,
10);
acceptor.setHandler(
new Demo1ServerHandler());
acceptor.bind(
new InetSocketAddress(PORT));
logger.info(
"服务器启动成功... 端口号未:"+PORT);
}
catch(Exception e){
logger.info(
"服务器启动异常..."+e.getMessage().toString());
e.printStackTrace();
}
}
}
2.2管理服务代码
public class Demo1ServerHandler extends IoHandlerAdapter {
public static Logger logger = Logger.getLogger(Demo1ServerHandler.class +
"");
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String msg = message.toString();
System.out.println(msg);
if (
"exit".equals(msg)) {
session.close(
true);
}
Date date =
new Date();
session.write(date);
logger.info(
"服务器接受消息成功..." + msg);
super.messageReceived(session, message);
}
@Override
public void messageSent(IoSession session, Object message)
throws Exception {
logger.info(
"服务器发送消息成功...");
super.messageSent(session, message);
}
@Override
public void sessionClosed(IoSession session)
throws Exception {
logger.info(
"服务器与客户端断开连接...");
super.sessionClosed(session);
}
@Override
public void sessionCreated(IoSession session)
throws Exception {
logger.info(
"服务器与客户端创建连接...");
super.sessionCreated(session);
}
@Override
public void sessionOpened(IoSession session)
throws Exception {
logger.info(
"服务器与客户端连接打开...");
super.sessionOpened(session);
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
logger.info(
"服务器进入空闲状态...");
super.sessionIdle(session, status);
}
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
logger.info(
"服务器发送异常...");
super.exceptionCaught(session, cause);
}
}
三、Android客户端
创建一个基于TCP/IP的非阻塞的clinet端的Socket编写过滤器(使用mina提供的文本换行符编解码器)设置连接超时为接收器设置管理服务连接服务器
3.1长连接客户端代码
注意: 1. Android的网络操作放在子线程 2. 清单文件需要添加访问网络的权限
public class MinaThread extends Thread {
private IoSession session =
null;
@Override
public void run() {
IoConnector connector=
new NioSocketConnector();
connector.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(
new TextLineCodecFactory(
Charset.forName(
"UTF-8"),
WINDOWS.getValue(),
WINDOWS.getValue()
)
)
);
connector.setConnectTimeoutMillis(
30000);
connector.setHandler(
new MinaClientHandler());
connector.connect(
new InetSocketAddress(ConstantUtil.IP_ADDRESS, ConstantUtil.PORT));
}
}
3.2管理服务代码
public class MinaClientHandler extends IoHandlerAdapter {
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String msg = message.toString();
Log.d(
"TEST",
"客户端接收到的信息为:" + msg);
super.messageReceived(session, message);
}
@Override
public void sessionOpened(IoSession session)
throws Exception {
session.write(
"this is client");
super.sessionOpened(session);
}
@Override
public void messageSent(IoSession session, Object message)
throws Exception {
super.messageSent(session, message);
}
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
Log.d(
"TEST",
"客户端发生异常");
super.exceptionCaught(session, cause);
}
}
4.工程链接
APP长连接工程地址_GitHub
转载请注明原文地址: https://ju.6miu.com/read-1371.html