asmack自身已经包括了断网重连的方案,客户端不需要自己再做重连方案,但是需要几个设置,如下所示:
configuration.setReconnectionAllowed(true); // 允许重连
configuration.setSendPresence(true);// 允许默认发送出息状态
// 在连接单例中初始化重连管理类
static{
try{
Class.forName("org.jivesoftware.smack.ReconnectionManager");
}catch(Exception e){
e.printStackTrace();
}
}
设计建议:网络连接类connection最好以单例形式存在