优化连接

This commit is contained in:
SL 2023-06-11 15:16:50 +08:00
parent 8bf6ceb042
commit 1be9507dca
41 changed files with 288 additions and 150 deletions

1
.idea/gradle.xml generated
View File

@ -18,5 +18,6 @@
</option>
</GradleProjectSettings>
</option>
<option name="offlineMode" value="true" />
</component>
</project>

View File

@ -0,0 +1,8 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,com.wukong.im.db.WKDBHelper,rawQuery" />
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1 @@
2574a1a954394bc0e14512c8c9130d29

View File

@ -0,0 +1 @@
400b72d0de18580ae143d524e08c3b67010b44be

View File

@ -0,0 +1 @@
5ace1b9a36a5370e07e744f9431064021db7d362ef58620f5868d5932c823c21

View File

@ -0,0 +1 @@
84d94b8f261faf5aedf1a4e7be07c953fdc0e6c6d37639c1640a8fe62924d9cacba636c97767cf2355bb3736b1a40f8bbad33e6bfa0e46ca6cff23df437c355a

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wukong.im</groupId>
<artifactId>WKIMLib_loca</artifactId>
<version>1.0.0</version>
<packaging>aar</packaging>
</project>

View File

@ -0,0 +1 @@
3ba12068a79874b5d3c0630df3e9424a

View File

@ -0,0 +1 @@
661ca872e7597a96bae9bb32206029fda635c03a

View File

@ -0,0 +1 @@
dd3e739be498f556545ae3c39336dc876f5ad6dce006e3fdf83044087989592e

View File

@ -0,0 +1 @@
b2ae240f103954a7b5a2d3f2d75dd8cc248f57365ec7b570af7c2c886fe018989903d12cb45733b5ee1923e0e4262fff1ba2c0f7d84edc191ed252543daf4d6c

View File

@ -0,0 +1 @@
a834d1c870e1b58d4530b187a1c568a9

View File

@ -0,0 +1 @@
130d3588b00c2e6b7798da9c05cd6b7db58417f5

View File

@ -0,0 +1 @@
681dbddd2958c98592015fa8b037bd7766b09c77e786db6867018f98ea0d6a10

View File

@ -0,0 +1 @@
058dd5210889a9b24ae14826adcbc00f02aa86fb8eca7eec8bb4e6ec864cc56cca0859e73a8c6ded84363b6354f2d790634efaf702c9e2dc8019a6a1c161c2ec

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wukong.im</groupId>
<artifactId>WKIMLib_loca</artifactId>
<version>1.0.1</version>
<packaging>aar</packaging>
</project>

View File

@ -0,0 +1 @@
ae5443a8ec63e4a3c4e4c75b011d8329

View File

@ -0,0 +1 @@
c54007b4b6d9c943e65452a032cf5f0f3fad7f1c

View File

@ -0,0 +1 @@
8284daf4ec8bbe6079a4c956ad7870c739e9724157dd3b7a82e4381b4941e68e

View File

@ -0,0 +1 @@
71f01502b151fe8b755106e2cc88d9b1a106b04c527515d977e3a15d2803421f6fa05c3fcfdc746fcd15d5867566a4294743532b8065951617afddcd5c2d3499

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>com.wukong.im</groupId>
<artifactId>WKIMLib_loca</artifactId>
<versioning>
<latest>1.0.1</latest>
<release>1.0.1</release>
<versions>
<version>1.0.0</version>
<version>1.0.1</version>
</versions>
<lastUpdated>20230609162512</lastUpdated>
</versioning>
</metadata>

View File

@ -0,0 +1 @@
d8d7749f8b5c0d9dfb58ce497eb00966

View File

@ -0,0 +1 @@
7b0a4a0d8ae72d7d20fa5917d47098bb72996ad4

View File

@ -0,0 +1 @@
9ce496a230bd923be4da34abd5f3501fe8dd686e8dd5e72b82515ab84e32a668

View File

@ -0,0 +1 @@
6279bd9f456efb566b4707ef2fe43d19f0ef8adc8f92cbba1fd497e5d55da8dc70b5c7096d76079a409d85e32ddc51938c751d69b24fd9a78a488087cbcac892

View File

@ -11,7 +11,7 @@ afterEvaluate {
afterEvaluate { artifact(tasks.getByName("bundleReleaseAar")) }
groupId = 'com.wukong.im'
artifactId = 'WKIMLib_loca'
version = '1.0.0'
version = '1.0.1'
}
}
repositories {

View File

@ -1,5 +1,8 @@
package com.wukong.im.db;
import static com.wukong.im.db.WKDBColumns.TABLE.channel;
import static com.wukong.im.db.WKDBColumns.TABLE.channelMembers;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;
@ -21,8 +24,6 @@ import java.util.List;
* channel DB manager
*/
public class ChannelDBManager {
private final String channel = "channel";
private final String channelMembers = "channel_members";
private ChannelDBManager() {
}
@ -74,24 +75,24 @@ public class ChannelDBManager {
selectionArgs[0] = channelId;
selectionArgs[1] = String.valueOf(channelType);
Cursor cursor = null;
WKChannel channel = null;
WKChannel wkChannel = null;
try {
cursor = WKIMApplication
.getInstance()
.getDbHelper()
.select(this.channel, selection, selectionArgs,
.select(channel, selection, selectionArgs,
null);
if (cursor != null) {
if (cursor.getCount() > 0) {
cursor.moveToNext();
channel = serializableChannel(cursor);
wkChannel = serializableChannel(cursor);
}
}
} finally {
if (cursor != null)
cursor.close();
}
return channel;
return wkChannel;
}
private boolean isExist(String channelId, int channelType) {
@ -162,29 +163,29 @@ public class ChannelDBManager {
}
}
private synchronized void insertChannel(WKChannel channel) {
private synchronized void insertChannel(WKChannel wkChannel) {
ContentValues cv = new ContentValues();
try {
cv = WKSqlContentValues.getContentValuesWithChannel(channel);
cv = WKSqlContentValues.getContentValuesWithChannel(wkChannel);
} catch (Exception e) {
e.printStackTrace();
}
WKIMApplication.getInstance().getDbHelper()
.insert(this.channel, cv);
.insert(channel, cv);
}
public synchronized void updateChannel(WKChannel channel) {
public synchronized void updateChannel(WKChannel wkChannel) {
String[] update = new String[2];
update[0] = channel.channelID;
update[1] = String.valueOf(channel.channelType);
update[0] = wkChannel.channelID;
update[1] = String.valueOf(wkChannel.channelType);
ContentValues cv = new ContentValues();
try {
cv = WKSqlContentValues.getContentValuesWithChannel(channel);
cv = WKSqlContentValues.getContentValuesWithChannel(wkChannel);
} catch (Exception e) {
e.printStackTrace();
}
WKIMApplication.getInstance().getDbHelper()
.update(this.channel, cv, WKDBColumns.WKChannelColumns.channel_id + "=? and " + WKDBColumns.WKChannelColumns.channel_type + "=?", update);
.update(channel, cv, WKDBColumns.WKChannelColumns.channel_id + "=? and " + WKDBColumns.WKChannelColumns.channel_type + "=?", update);
}

View File

@ -1,5 +1,8 @@
package com.wukong.im.db;
import static com.wukong.im.db.WKDBColumns.TABLE.channel;
import static com.wukong.im.db.WKDBColumns.TABLE.channelMembers;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;
@ -21,9 +24,7 @@ import java.util.List;
* 频道成员数据管理
*/
public class ChannelMembersDbManager {
final String channelMembers = "channel_members";
final String channelTab = "channel";
final String channelCols = "" + channelTab + ".channel_remark," + channelTab + ".channel_name," + channelTab + ".avatar," + channelTab + ".avatar_cache_key";
final String channelCols = "" + channel + ".channel_remark," + channel + ".channel_name," + channel + ".avatar," + channel + ".avatar_cache_key";
private ChannelMembersDbManager() {
}
@ -38,7 +39,7 @@ public class ChannelMembersDbManager {
public synchronized List<WKChannelMember> search(String channelId, byte channelType, String keyword, int page, int size) {
int queryPage = (page - 1) * size;
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channelTab + " on " + channelMembers + ".member_uid=" + channelTab + ".channel_id and " + channelTab + ".channel_type=1 where " + channelMembers + ".channel_id=" + "\"" + channelId + "\"" + " and " + channelMembers + ".channel_type=" + channelType + " and " + channelMembers + ".is_deleted=0 and " + channelMembers + ".status=1 and (member_name like '%" + keyword + "%' or member_remark like '%" + keyword + "%' or channel_name like '%" + keyword + "%' or channel_remark like '%" + keyword + "%') order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc limit " + queryPage + "," + size + "";
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channel + " on " + channelMembers + ".member_uid=" + channel + ".channel_id and " + channel + ".channel_type=1 where " + channelMembers + ".channel_id=" + "\"" + channelId + "\"" + " and " + channelMembers + ".channel_type=" + channelType + " and " + channelMembers + ".is_deleted=0 and " + channelMembers + ".status=1 and (member_name like '%" + keyword + "%' or member_remark like '%" + keyword + "%' or channel_name like '%" + keyword + "%' or channel_remark like '%" + keyword + "%') order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc limit " + queryPage + "," + size + "";
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql);
@ -55,7 +56,7 @@ public class ChannelMembersDbManager {
public synchronized List<WKChannelMember> queryWithPage(String channelId, byte channelType, int page, int size) {
int queryPage = (page - 1) * size;
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channelTab + " on " + channelMembers + ".member_uid=" + channelTab + ".channel_id and " + channelTab + ".channel_type=1 where " + channelMembers + ".channel_id=" + "\"" + channelId + "\"" + " and " + channelMembers + ".channel_type=" + channelType + " and " + channelMembers + ".is_deleted=0 and " + channelMembers + ".status=1 order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc limit " + queryPage + "," + size + "";
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channel + " on " + channelMembers + ".member_uid=" + channel + ".channel_id and " + channel + ".channel_type=1 where " + channelMembers + ".channel_id=" + "\"" + channelId + "\"" + " and " + channelMembers + ".channel_type=" + channelType + " and " + channelMembers + ".is_deleted=0 and " + channelMembers + ".status=1 order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc limit " + queryPage + "," + size + "";
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql);
@ -77,7 +78,7 @@ public class ChannelMembersDbManager {
* @return List<WKChannelMember>
*/
public synchronized List<WKChannelMember> query(String channelId, byte channelType) {
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channelTab + " on " + channelMembers + ".member_uid=" + channelTab + ".channel_id and " + channelTab + ".channel_type=1 where " + channelMembers + ".channel_id=" + "\"" + channelId + "\"" + " and " + channelMembers + ".channel_type=" + channelType + " and " + channelMembers + ".is_deleted=0 and " + channelMembers + ".status=1 order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc";
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channel + " on " + channelMembers + ".member_uid=" + channel + ".channel_id and " + channel + ".channel_type=1 where " + channelMembers + ".channel_id=" + "\"" + channelId + "\"" + " and " + channelMembers + ".channel_type=" + channelType + " and " + channelMembers + ".is_deleted=0 and " + channelMembers + ".status=1 order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc";
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql);
@ -93,7 +94,7 @@ public class ChannelMembersDbManager {
}
public synchronized List<WKChannelMember> queryDeleted(String channelId, byte channelType) {
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channelTab + " on " + channelMembers + ".member_uid=" + channelTab + ".channel_id and " + channelTab + ".channel_type=1 where " + channelMembers + ".channel_id=" + "\"" + channelId + "\"" + " and " + channelMembers + ".channel_type=" + channelType + " and " + channelMembers + ".is_deleted=1 and " + channelMembers + ".status=1 order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc";
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " LEFT JOIN " + channel + " on " + channelMembers + ".member_uid=" + channel + ".channel_id and " + channel + ".channel_type=1 where " + channelMembers + ".channel_id=" + "\"" + channelId + "\"" + " and " + channelMembers + ".channel_type=" + channelType + " and " + channelMembers + ".is_deleted=1 and " + channelMembers + ".status=1 order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc";
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql);
@ -110,7 +111,7 @@ public class ChannelMembersDbManager {
public synchronized boolean isExist(String channelId, byte channelType, String uid) {
boolean isExist = false;
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " left join " + channelTab + " on " + channelMembers + ".member_uid = " + channelTab + ".channel_id AND " + channelTab + ".channel_type=1 where (" + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.channel_id + "=" + "\"" + channelId + "\"" + " and " + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.channel_type + "=" + channelType + " and " + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.member_uid + "=" + "\"" + uid + "\")";
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " left join " + channel + " on " + channelMembers + ".member_uid = " + channel + ".channel_id AND " + channel + ".channel_type=1 where (" + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.channel_id + "=" + "\"" + channelId + "\"" + " and " + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.channel_type + "=" + channelType + " and " + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.member_uid + "=" + "\"" + uid + "\")";
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql)) {
@ -156,7 +157,7 @@ public class ChannelMembersDbManager {
*/
public synchronized WKChannelMember query(String channelId, byte channelType, String uid) {
WKChannelMember wkChannelMember = null;
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " left join " + channelTab + " on " + channelMembers + ".member_uid = " + channelTab + ".channel_id AND " + channelTab + ".channel_type=1 where (" + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.channel_id + "=" + "\"" + channelId + "\"" + " and " + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.channel_type + "=" + channelType + " and " + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.member_uid + "=" + "\"" + uid + "\")";
String sql = "select " + channelMembers + ".*," + channelCols + " from " + channelMembers + " left join " + channel + " on " + channelMembers + ".member_uid = " + channel + ".channel_id AND " + channel + ".channel_type=1 where (" + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.channel_id + "=" + "\"" + channelId + "\"" + " and " + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.channel_type + "=" + channelType + " and " + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.member_uid + "=" + "\"" + uid + "\")";
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql)) {
@ -435,7 +436,7 @@ public class ChannelMembersDbManager {
}
public synchronized List<WKChannelMember> queryChannelMembersByStatus(String channelId, byte channelType, int status) {
String sql = "select " + channelMembers + ".*," + channelTab + ".channel_name," + channelTab + ".channel_remark," + channelTab + ".avatar from " + channelMembers + " left Join " + channelTab + " where " + channelMembers + ".member_uid = " + channelTab + ".channel_id AND " + channelTab + ".channel_type=1 AND " + channelMembers + ".channel_id=" + "\"" + channelId + "\"" + " and " + channelMembers + ".channel_type=" + channelType + " and " + channelMembers + ".status=" + status + " order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc";
String sql = "select " + channelMembers + ".*," + channel + ".channel_name," + channel + ".channel_remark," + channel + ".avatar from " + channelMembers + " left Join " + channel + " where " + channelMembers + ".member_uid = " + channel + ".channel_id AND " + channel + ".channel_type=1 AND " + channelMembers + ".channel_id=" + "\"" + channelId + "\"" + " and " + channelMembers + ".channel_type=" + channelType + " and " + channelMembers + ".status=" + status + " order by " + channelMembers + ".role=1 desc," + channelMembers + ".role=2 desc," + channelMembers + "." + WKDBColumns.WKChannelMembersColumns.created_at + " asc";
Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql);

View File

@ -1,5 +1,10 @@
package com.wukong.im.db;
import static com.wukong.im.db.WKDBColumns.TABLE.channel;
import static com.wukong.im.db.WKDBColumns.TABLE.conversation;
import static com.wukong.im.db.WKDBColumns.TABLE.conversationExtra;
import static com.wukong.im.db.WKDBColumns.TABLE.message;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;
@ -13,7 +18,6 @@ import com.wukong.im.entity.WKConversationMsgExtra;
import com.wukong.im.entity.WKMsg;
import com.wukong.im.entity.WKUIConversationMsg;
import com.wukong.im.manager.ConversationManager;
import com.wukong.im.utils.WKLoggerUtils;
import org.json.JSONException;
import org.json.JSONObject;
@ -28,10 +32,6 @@ import java.util.List;
* 最近会话
*/
public class ConversationDbManager {
private final String message = "message";
private final String conversation = "conversation";
private final String conversationExtra = "conversation_extra";
private final String channel = "channel";
private final String extraCols = "IFNULL(" + conversationExtra + ".browse_to,0) AS browse_to,IFNULL(" + conversationExtra + ".keep_message_seq,0) AS keep_message_seq,IFNULL(" + conversationExtra + ".keep_offset_y,0) AS keep_offset_y,IFNULL(" + conversationExtra + ".draft,'') AS draft,IFNULL(" + conversationExtra + ".version,0) AS extra_version";
private final String channelCols = channel + ".channel_remark," +
channel + ".channel_name," +
@ -75,7 +75,6 @@ public class ConversationDbManager {
+ conversation + ".channel_id = " + channel + ".channel_id AND "
+ conversation + ".channel_type = " + channel + ".channel_type LEFT JOIN " + conversationExtra + " ON " + conversation + ".channel_id=" + conversationExtra + ".channel_id AND " + conversation + ".channel_type=" + conversationExtra + ".channel_type where " + conversation + ".is_deleted=0 order by "
+ WKDBColumns.WKCoverMessageColumns.last_msg_timestamp + " desc";
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper()
@ -83,7 +82,6 @@ public class ConversationDbManager {
if (cursor == null) {
return list;
}
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
WKConversationMsg msg = serializeMsg(cursor);
if (msg.isDeleted == 0) {

View File

@ -1,5 +1,10 @@
package com.wukong.im.db;
import static com.wukong.im.db.WKDBColumns.TABLE.channel;
import static com.wukong.im.db.WKDBColumns.TABLE.channelMembers;
import static com.wukong.im.db.WKDBColumns.TABLE.message;
import static com.wukong.im.db.WKDBColumns.TABLE.messageExtra;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;
@ -35,9 +40,6 @@ import java.util.List;
* 消息管理
*/
public class MsgDbManager {
private final String message = "message";
private final String messageExtra = "message_extra";
private final String channel = "channel";
private final String extraCols = "IFNULL(" + messageExtra + ".readed,0) as readed,IFNULL(" + messageExtra + ".readed_count,0) as readed_count,IFNULL(" + messageExtra + ".unread_count,0) as unread_count,IFNULL(" + messageExtra + ".revoke,0) as revoke,IFNULL(" + messageExtra + ".revoker,'') as revoker,IFNULL(" + messageExtra + ".extra_version,0) as extra_version,IFNULL(" + messageExtra + ".is_mutual_deleted,0) as is_mutual_deleted,IFNULL(" + messageExtra + ".content_edit,'') as content_edit,IFNULL(" + messageExtra + ".edited_at,0) as edited_at";
private final String messageCols = message + ".client_seq," + message + ".message_id," + message + ".message_seq," + message + ".channel_id," + message + ".channel_type," + message + ".timestamp," + message + ".from_uid," + message + ".type," + message + ".content," + message + ".status," + message + ".voice_status," + message + ".created_at," + message + ".updated_at," + message + ".searchable_word," + message + ".client_msg_no," + message + ".setting," + message + ".order_seq," + message + ".extra," + message + ".is_deleted," + message + ".flame," + message + ".flame_second," + message + ".viewed," + message + ".viewed_at";
@ -54,9 +56,9 @@ public class MsgDbManager {
private int requestCount;
public void getOrSyncHistoryMessages(String channelId, byte channelType, long oldestOrderSeq, boolean contain, boolean dropDown, int limit, final IGetOrSyncHistoryMsgBack iGetOrSyncHistoryMsgBack) {
public void getOrSyncHistoryMessages(String channelId, byte channelType, long oldestOrderSeq, boolean contain, int pullMode, int limit, final IGetOrSyncHistoryMsgBack iGetOrSyncHistoryMsgBack) {
//获取原始数据
List<WKMsg> list = getMessages(channelId, channelType, oldestOrderSeq, contain, dropDown, limit);
List<WKMsg> list = getMessages(channelId, channelType, oldestOrderSeq, contain, pullMode, limit);
//业务判断数据
List<WKMsg> tempList = new ArrayList<>();
@ -83,31 +85,38 @@ public class MsgDbManager {
boolean isSyncMsg = false;
//reverse false从区间大值开始拉取true从区间小值开始拉取
boolean reverse = false;
long startMsgSeq = 0;
long endMsgSeq = 0;
//同步消息的最大messageSeq
long syncMaxMsgSeq = 0;
// long syncMaxMsgSeq = 0;
//同步消息最小messageSeq,
long syncMinMsgSeq = 0;
// long syncMinMsgSeq = 0;
//判断页与页之间是否连续
long oldestMsgSeq;
//如果获取到的messageSeq为0说明oldestOrderSeq这条消息是本地消息则获取他上一条或下一条消息的messageSeq做为判断
if (oldestOrderSeq % 1000 != 0)
oldestMsgSeq = getMsgSeq(channelId, channelType, oldestOrderSeq, dropDown);
oldestMsgSeq = getMsgSeq(channelId, channelType, oldestOrderSeq, pullMode);
else oldestMsgSeq = oldestOrderSeq / 1000;
if (dropDown) {
if (pullMode == 0) {
//下拉获取消息
if (maxMessageSeq != 0 && oldestMsgSeq != 0 && oldestMsgSeq - maxMessageSeq > 1) {
isSyncMsg = true;
syncMaxMsgSeq = oldestMsgSeq;
syncMinMsgSeq = maxMessageSeq;
// syncMaxMsgSeq = oldestMsgSeq;
// syncMinMsgSeq = maxMessageSeq;
startMsgSeq = maxMessageSeq;
endMsgSeq = oldestMsgSeq;
reverse = false;//区间大值开始获取
}
} else {
//上拉获取消息
if (minMessageSeq != 0 && oldestMsgSeq != 0 && minMessageSeq - oldestMsgSeq > 1) {
isSyncMsg = true;
syncMaxMsgSeq = minMessageSeq;
syncMinMsgSeq = oldestMsgSeq;
// syncMaxMsgSeq = minMessageSeq;
// syncMinMsgSeq = oldestMsgSeq;
startMsgSeq = minMessageSeq;
endMsgSeq = oldestMsgSeq;
reverse = true;//区间小值开始获取
}
}
@ -123,10 +132,24 @@ public class MsgDbManager {
int num = getDeletedCount(tempList.get(i).messageSeq, tempList.get(nextIndex).messageSeq, channelId, channelType);
if (num < (tempList.get(nextIndex).messageSeq - tempList.get(i).messageSeq) - 1) {
isSyncMsg = true;
syncMaxMsgSeq = tempList.get(nextIndex).messageSeq;
syncMinMsgSeq = tempList.get(i).messageSeq;
if (dropDown) reverse = false;//区间大值开始获取
else reverse = true;//区间小值开始获取
// syncMaxMsgSeq = tempList.get(nextIndex).messageSeq;
// syncMinMsgSeq = tempList.get(i).messageSeq;
long max = tempList.get(nextIndex).messageSeq;
long min = tempList.get(i).messageSeq;
if (tempList.get(nextIndex).messageSeq < tempList.get(i).messageSeq) {
max = tempList.get(i).messageSeq;
min = tempList.get(nextIndex).messageSeq;
}
if (pullMode == 0) {
// 下拉
startMsgSeq = max;
endMsgSeq = min;
} else {
startMsgSeq = min;
endMsgSeq = max;
}
// if (pullMode == 0) reverse = false;//区间大值开始获取
// else reverse = true;//区间小值开始获取
break;
}
}
@ -143,18 +166,22 @@ public class MsgDbManager {
}
//计算最后一页后是否还存在消息
if (!isSyncMsg && tempList.size() < limit) {
if (dropDown) {
if (pullMode == 0) {
//如果下拉获取数据
isSyncMsg = true;
reverse = false;//从区间大值开始获取数据
syncMinMsgSeq = 0;
syncMaxMsgSeq = oldestMsgSeq;
// syncMinMsgSeq = 0;
// syncMaxMsgSeq = oldestMsgSeq;
startMsgSeq = oldestMsgSeq;
endMsgSeq = 0;
} else {
//如果上拉获取数据
isSyncMsg = true;
reverse = true;//从区间小值开始获取数据
syncMaxMsgSeq = 0;
syncMinMsgSeq = maxMessageSeq;
// syncMaxMsgSeq = 0;
// syncMinMsgSeq = maxMessageSeq;
startMsgSeq = oldestMsgSeq;
endMsgSeq = 0;
}
}
// if (!isContain) {
@ -184,12 +211,12 @@ public class MsgDbManager {
// }
// }
if (isSyncMsg && syncMaxMsgSeq != syncMinMsgSeq && syncMaxMsgSeq != 0 && requestCount < 5) {
if (isSyncMsg && startMsgSeq != endMsgSeq && requestCount < 5) {
//同步消息
requestCount++;
MsgManager.getInstance().setSyncChannelMsgListener(channelId, channelType, syncMinMsgSeq, syncMaxMsgSeq, limit, reverse, syncChannelMsg -> {
MsgManager.getInstance().setSyncChannelMsgListener(channelId, channelType, startMsgSeq, endMsgSeq, limit, pullMode, syncChannelMsg -> {
if (syncChannelMsg != null && syncChannelMsg.messages != null && syncChannelMsg.messages.size() > 0) {
getOrSyncHistoryMessages(channelId, channelType, oldestOrderSeq, contain, dropDown, limit, iGetOrSyncHistoryMsgBack);
getOrSyncHistoryMessages(channelId, channelType, oldestOrderSeq, contain, pullMode, limit, iGetOrSyncHistoryMsgBack);
} else {
requestCount = 0;
iGetOrSyncHistoryMsgBack.onResult(list);
@ -247,14 +274,14 @@ public class MsgDbManager {
return num;
}
private List<WKMsg> getMessages(String channelId, byte channelType, long oldestOrderSeq, boolean contain, boolean dropDown, int limit) {
private List<WKMsg> getMessages(String channelId, byte channelType, long oldestOrderSeq, boolean contain, int pullMode, int limit) {
List<WKMsg> msgList = new ArrayList<>();
String sql;
if (oldestOrderSeq <= 0) {
sql = "SELECT * FROM (SELECT " + messageCols + "," + extraCols + " FROM " + message + " LEFT JOIN " + messageExtra + " on " + message + ".message_id=" + messageExtra + ".message_id WHERE " + message + ".channel_id='" + channelId + "' and " + message + ".channel_type=" + channelType + " and " + message + ".type<>0 and " + message + ".type<>99) where is_deleted=0 and is_mutual_deleted=0 order by order_seq desc limit 0," + limit;
} else {
if (dropDown) {
if (pullMode == 0) {
sql = "SELECT * FROM (SELECT " + messageCols + "," + extraCols + " FROM " + message + " LEFT JOIN " + messageExtra + " on " + message + ".message_id=" + messageExtra + ".message_id WHERE " + message + ".channel_id='" + channelId + "' and " + message + ".channel_type=" + channelType + " and " + message + ".type<>0 and " + message + ".type<>99 AND " + message + ".order_seq<" + oldestOrderSeq + ") where is_deleted=0 and is_mutual_deleted=0 order by order_seq desc limit 0," + limit;
} else {
if (contain) {
@ -283,7 +310,7 @@ public class MsgDbManager {
}
if (!TextUtils.isEmpty(wkMsg.fromUID))
fromUIDs.add(wkMsg.fromUID);
if (dropDown)
if (pullMode == 0)
msgList.add(0, wkMsg);
else msgList.add(wkMsg);
}
@ -709,6 +736,7 @@ public class MsgDbManager {
return msg;
}
/**
* 删除消息
*
@ -1192,10 +1220,10 @@ public class MsgDbManager {
return messageSeq;
}
private int getMsgSeq(String channelID, byte channelType, long oldestOrderSeq, boolean dropDown) {
private int getMsgSeq(String channelID, byte channelType, long oldestOrderSeq, int pullMode) {
String sql;
int messageSeq = 0;
if (!dropDown) {
if (pullMode == 1) {
sql = "select * from " + message + " where channel_id=" + "\"" + channelID + "\"" + " and channel_type=" + channelType + " and order_seq>" + oldestOrderSeq + " and message_seq<>0 order by message_seq desc limit 1";
} else
sql = "select * from " + message + " where channel_id=" + "\"" + channelID + "\"" + " and channel_type=" + channelType + " and order_seq<" + oldestOrderSeq + " and message_seq<>0 order by message_seq asc limit 1";

View File

@ -1,5 +1,7 @@
package com.wukong.im.db;
import static com.wukong.im.db.WKDBColumns.TABLE.messageReaction;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;
@ -18,7 +20,6 @@ import java.util.List;
* 消息回应
*/
class MsgReactionDBManager {
final String messageReaction = "message_reaction";
private MsgReactionDBManager() {
}

View File

@ -1,5 +1,7 @@
package com.wukong.im.db;
import static com.wukong.im.db.WKDBColumns.TABLE.reminders;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;
@ -18,8 +20,6 @@ import java.util.Iterator;
import java.util.List;
public class ReminderDBManager {
private final String tab = "reminders";
private ReminderDBManager() {
}
@ -32,7 +32,7 @@ public class ReminderDBManager {
}
public long getMaxVersion() {
String sql = "select * from " + tab + " order by version desc limit 1";
String sql = "select * from " + reminders + " order by version desc limit 1";
long version = 0;
try (Cursor cursor = WKIMApplication
.getInstance()
@ -48,7 +48,7 @@ public class ReminderDBManager {
}
public List<WKReminder> queryWithChannel(String channelID, byte channelType, int done) {
String sql = "select * from " + tab + " where channel_id='" + channelID + "' and channel_type=" + channelType + " and done=" + done + " order by message_seq desc";
String sql = "select * from " + reminders + " where channel_id='" + channelID + "' and channel_type=" + channelType + " and done=" + done + " order by message_seq desc";
List<WKReminder> list = new ArrayList<>();
try (Cursor cursor = WKIMApplication.getInstance().getDbHelper().rawQuery(sql)) {
if (cursor == null) {
@ -63,7 +63,7 @@ public class ReminderDBManager {
}
public List<WKReminder> queryWithChannelAndType(String channelID, byte channelType, int done, int type) {
String sql = "select * from " + tab + " where channel_id='" + channelID + "' and channel_type=" + channelType + " and done=" + done + " and type =" + type + " order by message_seq desc";
String sql = "select * from " + reminders + " where channel_id='" + channelID + "' and channel_type=" + channelType + " and done=" + done + " and type =" + type + " order by message_seq desc";
List<WKReminder> list = new ArrayList<>();
try (Cursor cursor = WKIMApplication.getInstance().getDbHelper().rawQuery(sql)) {
if (cursor == null) {
@ -85,7 +85,7 @@ public class ReminderDBManager {
}
stringBuffer.append(ids.get(i));
}
String sql = "select * from " + tab + " where reminder_id in (" + stringBuffer + ")";
String sql = "select * from " + reminders + " where reminder_id in (" + stringBuffer + ")";
List<WKReminder> list = new ArrayList<>();
try (Cursor cursor = WKIMApplication
.getInstance()
@ -111,7 +111,7 @@ public class ReminderDBManager {
}
stringBuffer.append(channelIds.get(i)).append("'");
}
String sql = "select * from " + tab + " where channel_id in ('" + stringBuffer + ")";
String sql = "select * from " + reminders + " where channel_id in ('" + stringBuffer + ")";
List<WKReminder> list = new ArrayList<>();
try (Cursor cursor = WKIMApplication
.getInstance()
@ -165,7 +165,7 @@ public class ReminderDBManager {
.beginTransaction();
if (insertCVs.size() > 0) {
for (ContentValues cv : insertCVs) {
WKIMApplication.getInstance().getDbHelper().insert(tab, cv);
WKIMApplication.getInstance().getDbHelper().insert(reminders, cv);
}
}
if (updateCVs.size() > 0) {
@ -173,7 +173,7 @@ public class ReminderDBManager {
String[] update = new String[1];
update[0] = cv.getAsString("reminder_id");
WKIMApplication.getInstance().getDbHelper()
.update(tab, cv, "reminder_id=?", update);
.update(reminders, cv, "reminder_id=?", update);
}
}
WKIMApplication.getInstance().getDbHelper().getDb()

View File

@ -1,5 +1,8 @@
package com.wukong.im.db;
import static com.wukong.im.db.WKDBColumns.TABLE.robot;
import static com.wukong.im.db.WKDBColumns.TABLE.robotMenu;
import android.content.ContentValues;
import android.database.Cursor;
@ -11,8 +14,6 @@ import java.util.ArrayList;
import java.util.List;
public class RobotDBManager {
private final String robot = "robot";
private final String robotMenu = "robot_menu";
private RobotDBManager() {
}
@ -26,11 +27,11 @@ public class RobotDBManager {
}
public void insertOrUpdateMenu(List<WKRobotMenu> list) {
for (WKRobotMenu robotMenu : list) {
if (isExitMenu(robotMenu.robotID, robotMenu.cmd)) {
update(robotMenu);
for (WKRobotMenu menu : list) {
if (isExitMenu(menu.robotID, menu.cmd)) {
update(menu);
} else {
WKIMApplication.getInstance().getDbHelper().insert(this.robotMenu, getCV(robotMenu));
WKIMApplication.getInstance().getDbHelper().insert(robotMenu, getCV(menu));
}
}
}
@ -88,33 +89,33 @@ public class RobotDBManager {
return isExist;
}
private void update(WKRobot robot) {
private void update(WKRobot wkRobot) {
String[] updateKey = new String[6];
String[] updateValue = new String[6];
updateKey[0] = "status";
updateValue[0] = String.valueOf(robot.status);
updateValue[0] = String.valueOf(wkRobot.status);
updateKey[1] = "version";
updateValue[1] = String.valueOf(robot.version);
updateValue[1] = String.valueOf(wkRobot.version);
updateKey[2] = "updated_at";
updateValue[2] = String.valueOf(robot.updatedAT);
updateValue[2] = String.valueOf(wkRobot.updatedAT);
updateKey[3] = "username";
updateValue[3] = robot.username;
updateValue[3] = wkRobot.username;
updateKey[4] = "placeholder";
updateValue[4] = robot.placeholder;
updateValue[4] = wkRobot.placeholder;
updateKey[5] = "inline_on";
updateValue[5] = String.valueOf(robot.inlineOn);
updateValue[5] = String.valueOf(wkRobot.inlineOn);
String where = "robot_id=?";
String[] whereValue = new String[1];
whereValue[0] = robot.robotID;
whereValue[0] = wkRobot.robotID;
WKIMApplication.getInstance().getDbHelper()
.update(this.robot, updateKey, updateValue, where, whereValue);
.update(robot, updateKey, updateValue, where, whereValue);
}
private void insert(WKRobot robot) {
ContentValues cv = getCV(robot);
WKIMApplication.getInstance().getDbHelper().insert(this.robot, cv);
private void insert(WKRobot robot1) {
ContentValues cv = getCV(robot1);
WKIMApplication.getInstance().getDbHelper().insert(robot, cv);
}
public void insertRobots(List<WKRobot> list) {
@ -136,8 +137,8 @@ public class RobotDBManager {
}
public WKRobot query(String robotID) {
WKRobot robot = null;
String sql = "select * from " + this.robot + " where robot_id = " + "\"" + robotID + "\"";
WKRobot wkRobot = null;
String sql = "select * from " + robot + " where robot_id = " + "\"" + robotID + "\"";
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql)) {
@ -145,15 +146,15 @@ public class RobotDBManager {
return null;
}
if (cursor.moveToLast()) {
robot = serializeRobot(cursor);
wkRobot = serializeRobot(cursor);
}
}
return robot;
return wkRobot;
}
public WKRobot queryWithUsername(String username) {
WKRobot robot = null;
String sql = "select * from " + this.robot + " where username = " + "\"" + username + "\"";
WKRobot wkRobot = null;
String sql = "select * from " + robot + " where username = " + "\"" + username + "\"";
try (Cursor cursor = WKIMApplication
.getInstance()
.getDbHelper().rawQuery(sql)) {
@ -161,10 +162,10 @@ public class RobotDBManager {
return null;
}
if (cursor.moveToLast()) {
robot = serializeRobot(cursor);
wkRobot = serializeRobot(cursor);
}
}
return robot;
return wkRobot;
}
public List<WKRobot> queryRobots(List<String> robotIds) {

View File

@ -5,6 +5,18 @@ package com.wukong.im.db;
* 数据库字段
*/
public interface WKDBColumns {
class TABLE{
public static final String message = "message";
public static final String messageReaction = "message_reaction";
public static final String messageExtra = "message_extra";
public static final String conversation = "conversation";
public static final String conversationExtra = "conversation_extra";
public static final String channel = "channel";
public static final String channelMembers = "channel_members";
public static final String reminders = "reminders";
public static final String robot = "robot";
public static final String robotMenu = "robot_menu";
}
//频道db字段
class WKChannelColumns {
//自增ID

View File

@ -5,5 +5,5 @@ package com.wukong.im.interfaces;
* 同步频道消息
*/
public interface ISyncChannelMsgListener {
void syncChannelMsgs(String channelID, byte channelType, long minMessageSeq, long maxMesageSeq, int limit, boolean reverse, ISyncChannelMsgBack iSyncChannelMsgBack);
void syncChannelMsgs(String channelID, byte channelType, long startMessageSeq, long endMessageSeq, int limit, int pullMode, ISyncChannelMsgBack iSyncChannelMsgBack);
}

View File

@ -12,6 +12,7 @@ import com.wukong.im.message.MessageHandler;
import com.wukong.im.utils.WKLoggerUtils;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
/**
@ -80,9 +81,16 @@ public class ConnectionManager extends BaseManager {
WKIMApplication.getInstance().closeDbHelper();
}
public void getIpAndPort(IGetSocketIpAndPortListener iGetIpAndPortListener) {
public interface IRequestIP {
void onResult(String requestId, String ip, int port);
}
public void getIpAndPort(String requestId, IRequestIP iRequestIP) {
if (iGetIpAndPort != null) {
runOnMainThread(() -> iGetIpAndPort.getIP(iGetIpAndPortListener));
WKLoggerUtils.getInstance().e("获取IP中...");
runOnMainThread(() -> iGetIpAndPort.getIP((ip, port) -> iRequestIP.onResult(requestId, ip, port)));
} else {
WKLoggerUtils.getInstance().e("未注册获取IP事件");
}
}

View File

@ -285,19 +285,19 @@ public class MsgManager extends BaseManager {
* @param channelType 频道类型
* @param oldestOrderSeq 最后一次消息大orderSeq 第一次进入聊天传入0
* @param contain 是否包含 oldestOrderSeq 这条消息
* @param dropDown 是否下拉
* @param pullMode 拉取模式 0:向下拉取 1:向上拉取
* @param aroundMsgOrderSeq 查询此消息附近消息
* @param limit 每次获取数量
* @param iGetOrSyncHistoryMsgBack 请求返还
*/
public void getOrSyncHistoryMessages(String channelId, byte channelType, long oldestOrderSeq, boolean contain, boolean dropDown, int limit, long aroundMsgOrderSeq, final IGetOrSyncHistoryMsgBack iGetOrSyncHistoryMsgBack) {
public void getOrSyncHistoryMessages(String channelId, byte channelType, long oldestOrderSeq, boolean contain, int pullMode, int limit, long aroundMsgOrderSeq, final IGetOrSyncHistoryMsgBack iGetOrSyncHistoryMsgBack) {
if (aroundMsgOrderSeq != 0) {
long maxMsgSeq = getMaxMessageSeq(channelId, channelType);
long aroundMsgSeq = getOrNearbyMsgSeq(aroundMsgOrderSeq);
if (maxMsgSeq >= aroundMsgSeq && maxMsgSeq - aroundMsgSeq <= limit) {
oldestOrderSeq = 0;
contain = false;
dropDown = true;
pullMode = 0;
} else {
long minOrderSeq = MsgDbManager.getInstance().getOrderSeq(channelId, channelType, aroundMsgOrderSeq, 3);
if (minOrderSeq == 0) {
@ -318,11 +318,11 @@ public class MsgManager extends BaseManager {
oldestOrderSeq = startOrderSeq;
}
}
dropDown = false;
pullMode = 1;
contain = true;
}
}
MsgDbManager.getInstance().getOrSyncHistoryMessages(channelId, channelType, oldestOrderSeq, contain, dropDown, limit, iGetOrSyncHistoryMsgBack);
MsgDbManager.getInstance().getOrSyncHistoryMessages(channelId, channelType, oldestOrderSeq, contain, pullMode, limit, iGetOrSyncHistoryMsgBack);
}
public List<WKMsg> queryAll() {
@ -879,9 +879,9 @@ public class MsgManager extends BaseManager {
this.iSyncChannelMsgListener = listener;
}
public void setSyncChannelMsgListener(String channelID, byte channelType, long minMessageSeq, long maxMesageSeq, int limit, boolean reverse, ISyncChannelMsgBack iSyncChannelMsgBack) {
public void setSyncChannelMsgListener(String channelID, byte channelType, long startMessageSeq, long endMessageSeq, int limit, int pullMode, ISyncChannelMsgBack iSyncChannelMsgBack) {
if (this.iSyncChannelMsgListener != null) {
runOnMainThread(() -> iSyncChannelMsgListener.syncChannelMsgs(channelID, channelType, minMessageSeq, maxMesageSeq, limit, reverse, syncChannelMsg -> {
runOnMainThread(() -> iSyncChannelMsgListener.syncChannelMsgs(channelID, channelType, startMessageSeq, endMessageSeq, limit, pullMode, syncChannelMsg -> {
if (syncChannelMsg != null && syncChannelMsg.messages != null && syncChannelMsg.messages.size() > 0) {
saveSyncChannelMSGs(syncChannelMsg.messages);
}

View File

@ -34,7 +34,7 @@ class ClientHandler implements IDataHandler, IConnectHandler,
@Override
public boolean onConnectException(INonBlockingConnection iNonBlockingConnection, IOException e) {
WKLoggerUtils.getInstance().e("连接异常");
ConnectionHandler.getInstance().reconnection();
ConnectionHandler.getInstance().forcedReconnection();
close(iNonBlockingConnection);
return true;
}
@ -47,7 +47,7 @@ class ClientHandler implements IDataHandler, IConnectHandler,
if (ConnectionHandler.getInstance().connection != null && iNonBlockingConnection != null) {
if (!ConnectionHandler.getInstance().connection.getId().equals(iNonBlockingConnection.getId())) {
close(iNonBlockingConnection);
ConnectionHandler.getInstance().reconnection();
ConnectionHandler.getInstance().forcedReconnection();
} else {
//连接成功
isConnectSuccess = true;
@ -57,7 +57,7 @@ class ClientHandler implements IDataHandler, IConnectHandler,
} else {
close(iNonBlockingConnection);
WKLoggerUtils.getInstance().e("连接成功连接对象为空");
ConnectionHandler.getInstance().reconnection();
ConnectionHandler.getInstance().forcedReconnection();
}
return false;
}
@ -66,7 +66,7 @@ class ClientHandler implements IDataHandler, IConnectHandler,
public boolean onConnectionTimeout(INonBlockingConnection iNonBlockingConnection) {
if (!isConnectSuccess) {
WKLoggerUtils.getInstance().e("连接超时");
ConnectionHandler.getInstance().reconnection();
ConnectionHandler.getInstance().forcedReconnection();
}
return true;
}
@ -84,7 +84,7 @@ class ClientHandler implements IDataHandler, IConnectHandler,
e.printStackTrace();
}
ConnectionHandler.getInstance().reconnection();
ConnectionHandler.getInstance().forcedReconnection();
return true;
}
}
@ -124,7 +124,7 @@ class ClientHandler implements IDataHandler, IConnectHandler,
public boolean onDisconnect(INonBlockingConnection iNonBlockingConnection) {
WKLoggerUtils.getInstance().e("连接断开");
if (WKIMApplication.getInstance().isCanConnect) {
ConnectionHandler.getInstance().reconnection();
ConnectionHandler.getInstance().forcedReconnection();
}else {
WKLoggerUtils.getInstance().e("不能重连-->");
}
@ -136,7 +136,7 @@ class ClientHandler implements IDataHandler, IConnectHandler,
public boolean onIdleTimeout(INonBlockingConnection iNonBlockingConnection) {
if (!isConnectSuccess) {
WKLoggerUtils.getInstance().e("Idle连接超时");
ConnectionHandler.getInstance().reconnection();
ConnectionHandler.getInstance().forcedReconnection();
close(iNonBlockingConnection);
}
return true;

View File

@ -2,7 +2,6 @@ package com.wukong.im.message;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
@ -46,6 +45,8 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ -74,15 +75,25 @@ public class ConnectionHandler {
private long lastMsgTime = 0;
private String ip;
private int port;
INonBlockingConnection connection;
ClientHandler clientHandler;
volatile INonBlockingConnection connection;
volatile ClientHandler clientHandler;
private long requestIPTime;
private final long requestIPTimeoutTime = 6;
public String socketSingleID;
private String lastRequestId;
public synchronized void forcedReconnection() {
isReConnecting = false;
requestIPTime = 0;
reconnection();
}
public synchronized void reconnection() {
ip = "";
port = 0;
if (isReConnecting) {
long nowTime = DateUtils.getInstance().getCurrentSeconds();
if (nowTime - requestIPTime > 5) {
if (nowTime - requestIPTime > requestIPTimeoutTime) {
isReConnecting = false;
}
return;
@ -107,14 +118,14 @@ public class ConnectionHandler {
} catch (InterruptedException e) {
e.printStackTrace();
}
reconnection();
new Handler(Looper.getMainLooper()).post(() -> reconnection());
}
}.start();
}
}
}
private void getIPAndPort() {
private synchronized void getIPAndPort() {
if (!WKIMApplication.getInstance().isNetworkConnected()) {
isReConnecting = false;
reconnection();
@ -127,24 +138,29 @@ public class ConnectionHandler {
}
WKIM.getInstance().getConnectionManager().setConnectionStatus(WKConnectStatus.connecting, WKConnectReason.Connecting);
// 计算获取IP时长 todo
ConnectionManager.getInstance().getIpAndPort((ip, port) -> {
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer = null;
WKLoggerUtils.getInstance().e("取消请求socket IP倒计时器--->");
}
startRequestIPTimer();
lastRequestId = UUID.randomUUID().toString().replace("-", "");
ConnectionManager.getInstance().getIpAndPort(lastRequestId, (requestId, ip, port) -> {
if (TextUtils.isEmpty(ip) || port == 0) {
WKLoggerUtils.getInstance().e("返回连接IP或port错误" + String.format("ip:%s & port:%s", ip, port));
isReConnecting = false;
reconnection();
} else {
this.ip = ip;
this.port = port;
WKLoggerUtils.getInstance().e("连接的IP和Port" + ip + ":" + port);
new Thread(this::connSocket).start();
if (lastRequestId.equals(requestId)) {
ConnectionHandler.this.ip = ip;
ConnectionHandler.this.port = port;
WKLoggerUtils.getInstance().e("连接的IP和Port" + ip + ":" + port);
if (connectionIsNull()) {
new Thread(ConnectionHandler.this::connSocket).start();
}
} else {
if (connectionIsNull()) {
WKLoggerUtils.getInstance().e("请求IP的编号不一致重连中");
reconnection();
}
}
}
});
new Handler(Looper.getMainLooper()).post(this::startRequestIPTimer);
}
private void connSocket() {
@ -509,7 +525,7 @@ public class ConnectionHandler {
if (connection != null && connection.isOpen()) {
try {
WKLoggerUtils.getInstance().e("stop connection" + connection.getId());
connection.flush();
// connection.flush();
connection.close();
} catch (IOException e) {
e.printStackTrace();
@ -519,28 +535,39 @@ public class ConnectionHandler {
connection = null;
}
CountDownTimer countDownTimer;
private Timer checkNetWorkTimer;
private synchronized void startRequestIPTimer() {
if (countDownTimer != null) {
return;
if (checkNetWorkTimer != null) {
checkNetWorkTimer.cancel();
checkNetWorkTimer = null;
}
countDownTimer = new CountDownTimer(5000, 1000) {
checkNetWorkTimer = new Timer();
WKLoggerUtils.getInstance().e("开始计算IP请求时间");
checkNetWorkTimer.schedule(new TimerTask() {
@Override
public void onTick(long l) {
WKLoggerUtils.getInstance().e("请求socket IP倒计时中--->");
}
@Override
public void onFinish() {
if (connectionIsNull()) {
WKLoggerUtils.getInstance().e("请求socket IP已超时--->");
isReConnecting = false;
countDownTimer = null;
reconnection();
public void run() {
long nowTime = DateUtils.getInstance().getCurrentSeconds();
if (nowTime - requestIPTime >= requestIPTimeoutTime) {
checkNetWorkTimer.cancel();
checkNetWorkTimer.purge();
checkNetWorkTimer = null;
if (TextUtils.isEmpty(ip) || port == 0) {
WKLoggerUtils.getInstance().e("请求IP已超时开始重连--->");
isReConnecting = false;
reconnection();
}
} else {
if (!TextUtils.isEmpty(ip) && port != 0) {
checkNetWorkTimer.cancel();
checkNetWorkTimer.purge();
checkNetWorkTimer = null;
WKLoggerUtils.getInstance().e("请求IP倒计时已销毁--->");
} else {
WKLoggerUtils.getInstance().e("请求IP倒计时中--->" + (nowTime - requestIPTime));
}
}
}
};
}, 500, 1000L);
}
}