AndroidPN
https://github.com/dannytiehui/androidpn
郭霖 Android消息推送教学视频
http://www.imooc.com/learn/223
http://www.imooc.com/learn/358
Mina
官网 http://mina.apache.org/mina-project/index.html
资源
http://my.oschina.net/ielts0909/blog/92716?p=1#comments
http://my.oschina.net/makemyownlife/blog/122738
XMPP
官网 http://xmpp.org/
资源 http://www.cnblogs.com/hanyonglu/archive/2012/03/04/2378956.html
Mina
百度百科
ApacheMINA是一个网络应用程序框架,用来帮助用户简单地开发高性能和高可扩展性的网络应用程序。它提供了一个通过Java NIO在不同的传输例如TCP/IP和UDP/IP上抽象的事件驱动的异步API。
服务器
public class MinaTimeServer
{
public static void main( String[] args ) throws IOException
{
IoAcceptor acceptor = new NioSocketAcceptor();
//日志过滤器
acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
//ProtocolCodecFilter该过滤器将二进制或者协议特定的数据转换为消息对象
//TextLineCodecFactory处理文本信息(编解码处理)
acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter(new
TextLineCodecFactory( Charset.forName( "UTF-8" ))));
//设置逻辑处理器(服务于客户端的请求)
acceptor.setHandler( new TimeServerHandler() );
acceptor.getSessionConfig().setReadBufferSize( 2048 );
//设置空闲时间
acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
}
}
TimeServerHandler
public class TimeServerHandler extends IoHandlerAdapter
{
@Override
public void exceptionCaught( IoSession session, Throwable cause ) throws
Exception
{
cause.printStackTrace();
}
@Override
public void messageReceived( IoSession session, Object message ) throws Exception
{
//服务器接收请求
String str = message.toString();
if( str.trim().equalsIgnoreCase("quit") ) {
session.close();
return;
}
Date date = new Date();
session.write( date.toString() );
System.out.println("Message written...");
}
@Override
public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
{
//服务器进入空闲状态
System.out.println( "IDLE " + session.getIdleCount( status ));
}
sessionOpen()...//服务器与客户端连接开启
sessionCreated()...//服务器与客户端连接创建
sessionClose()...//服务器与客户端连接关闭
messageSent()...//服务器发送消息,消息发送事件触发,当消息即响应发送(调用IoSession.write())
}
自定义编解码
http://www.himigame.com/apache-mina/839.html
http://blog.youkuaiyun.com/a600423444/article/details/6671035
(以下代码出处 郭霖消息推送视频)
XmppDecoder
public class XmppDecoder extends CumulativeProtocolDecoder {
// private final Log log = LogFactory.getLog(XmppDecoder.class);
@Override
public boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
int startPosition = in.position();
while(in.hasRemaing()){
byte b = in.get();
if(b=='\n'){
int currentPosition = in.position();
int limit = in.limit();
in.position(startPosition);
in.limit(currentPosition);
IoBuffer buf= in.slince();
byte[] dest = new byte[buf.limit()];
buf.get(dest);
String str = new String(dest);
out.write(str);
in.position(currentPosition);
in.limit(limit);
}
}
}
}
XmppEncoder
public class XmppEncoder implements ProtocolEncoder {
public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
// log.debug("encode()...");
String s =null;
if(message instanceof String){
s = (String)message;
}
if(s!=null){
CharsetEncoder charsetEncoder =(CharsetEncoder)
session.getAttribute("encoder");
if(charsetEncoder==null){
charsetEncoder = Charset.defaultCharset().newEncoder();
session.setAttribute("encoder",charsetEncoder);
}
IoBuffer ioBuffer = IoBuffer.allocate(s.length);
ioBuffer.setAutoExpanded(true);
ioBuffer.putString(s,charsetEncoder);
ioBuffer.flip();
out.write(ioBuffer);
}
public void dispose(IoSession session) throws Exception {
// log.debug("dispose()...");
}
}
XmppFactory
public class XmppCodecFactory implements ProtocolCodecFactory {
private final XmppEncoder encoder;
private final XmppDecoder decoder;
/**
* Constructor.
*/
public XmppCodecFactory() {
encoder = new XmppEncoder();
decoder = new XmppDecoder();
}
/**
* Returns a new (or reusable) instance of ProtocolEncoder.
*/
public ProtocolEncoder getEncoder(IoSession session) throws Exception {
return encoder;
}
/**
* Returns a new (or reusable) instance of ProtocolDecoder.
*/
public ProtocolDecoder getDecoder