android asmack 重连分析
来源:互联网 发布:网络歌曲爱相随 编辑:程序博客网 时间:2024/05/17 23:54
在做asmack的时候,发现有重连机制,分析了一下。
1、设置重连属性
ConnectionConfiguration.setReconnectionAllowed(boolean isAllowed)
public void setReconnectionAllowed(boolean isAllowed) {
this.reconnectionAllowed = isAllowed;
设置属性后,再调用这个方法是,查看是否重连
public boolean isReconnectionAllowed() {
return this.reconnectionAllowed;
}
2、在什么时候重连
connectionClosedOnError
parsePackets(Thread thread)
protected void reconnect() {
if (this.isReconnectionAllowed()) {
// Since there is no thread running, creates a new one to attempt
// the reconnection.
Thread reconnectionThread = new Thread() {
/**
* Holds the current number of reconnection attempts
*/
private int attempts = 0;
/**
* Returns the number of seconds until the next reconnection attempt.
*这个地方值得学习啊
* @return the number of seconds until the next reconnection attempt.
*/
private int timeDelay() {
if (attempts > 13) {
return 60 * 5; // 5 minutes
}
if (attempts > 7) {
return 60; // 1 minute
}
return 10; // 10 seconds
}
/**
* The process will try the reconnection until the connection succeed or the user
* cancell it
*/
public void run() {
// The process will try to reconnect until the connection is established or
// the user cancel the reconnection process {@link Connection#disconnect()}
while (ReconnectionManager.this.isReconnectionAllowed()) {
// Find how much time we should wait until the next reconnection
int remainingSeconds = timeDelay();
// Sleep until we're ready for the next reconnection attempt. Notify
// listeners once per second about how much time remains before the next
// reconnection attempt.
while (ReconnectionManager.this.isReconnectionAllowed() &&
remainingSeconds > 0)
{
try {
Thread.sleep(1000);
remainingSeconds--;
ReconnectionManager.this
.notifyAttemptToReconnectIn(remainingSeconds);
}
catch (InterruptedException e1) {
e1.printStackTrace();
// Notify the reconnection has failed
ReconnectionManager.this.notifyReconnectionFailed(e1);
}
}
// Makes a reconnection attempt
try {
if (ReconnectionManager.this.isReconnectionAllowed()) {
connection.connect();
}
}
catch (XMPPException e) {
// Fires the failed reconnection notification
ReconnectionManager.this.notifyReconnectionFailed(e);
}
}
}
};
reconnectionThread.setName("Smack Reconnection Manager");
reconnectionThread.setDaemon(true);
reconnectionThread.start();
}
}
- android asmack 重连分析
- asmack自动重连
- android xmpp开发 asmack实现断线自动重连
- android xmpp开发 asmack实现断线自动重连
- android xmpp开发 asmack实现断线自动重连
- 解决asmack不能自动重连
- 解决asmack不能自动重连
- xmpp asmack重连的逻辑
- xmpp/aSmack源码分析(基于android客户端)
- androidM wifi重连分析
- Android Xmpp断线重连
- Android基于XMPP协议之asmack源码分析
- android之XMPP过程分析(连接、文件监听、登录、异常重连)
- aSmack源码分析PacketReader
- Android IM(五)连接断开重连
- 【android学习】断线重连机制
- android asmack 接受消息
- Android asmack使用介绍
- VC运行时库MD,MT,MTD,MDD的区别
- AE生成高清视频设置
- 用批处理将源文件夹下面的所有文件展开拷贝到指定目标文件
- 【上机作业】 第一次上机试题
- 2013年上半年系统集成项目管理工程师(软考)培训视频、资料、历年试题、电子书教程
- android asmack 重连分析
- 有关html的基础知识
- MySQL错误:Can't connect to MySQL server on localhost (10061)解决方法
- 初次见面
- dreamweaver 8 安装jQuery插件(扩展包)提示输入
- Linux系统的头文件和库文件搜索路径
- POJ 1125 Stockbroker Grapevine 图论 Floyd-Washall算法
- 选择机构的语句
- 商业智能平台研究:ETL中的数据质量控制