mirror of
https://github.com/WuKongIM/WuKongIMAndroidSDK
synced 2025-06-03 23:58:19 +00:00
fix:Optimize connections
This commit is contained in:
parent
dc2b1a9913
commit
8583c38317
@ -18,7 +18,7 @@ import com.xinbida.wukongim.utils.CryptoUtils;
|
||||
* 5/20/21 5:25 PM
|
||||
*/
|
||||
public class WKIM {
|
||||
private final String Version = "V1.1.9";
|
||||
private final String Version = "V1.2.3";
|
||||
|
||||
private WKIM() {
|
||||
|
||||
|
@ -62,6 +62,7 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
public class WKConnection {
|
||||
private final String TAG = "WKConnection";
|
||||
|
||||
private WKConnection() {
|
||||
}
|
||||
|
||||
@ -132,13 +133,8 @@ public class WKConnection {
|
||||
}
|
||||
|
||||
private synchronized void getIPAndPort() {
|
||||
if (!WKIMApplication.getInstance().isNetworkConnected()) {
|
||||
isReConnecting = false;
|
||||
reconnectionHandler.postDelayed(reconnectionRunnable, reconnectDelay);
|
||||
return;
|
||||
}
|
||||
if (!WKIMApplication.getInstance().isCanConnect) {
|
||||
WKLoggerUtils.getInstance().e(TAG,"SDK determines that reconnection is not possible");
|
||||
WKLoggerUtils.getInstance().e(TAG, "SDK determines that reconnection is not possible");
|
||||
stopAll();
|
||||
return;
|
||||
}
|
||||
@ -147,30 +143,29 @@ public class WKConnection {
|
||||
startRequestIPTimer();
|
||||
lastRequestId = UUID.randomUUID().toString().replace("-", "");
|
||||
ConnectionManager.getInstance().getIpAndPort(lastRequestId, (requestId, ip, port) -> {
|
||||
WKLoggerUtils.getInstance().e(TAG, "connection address " + ip + ":" + port);
|
||||
if (TextUtils.isEmpty(ip) || port == 0) {
|
||||
WKLoggerUtils.getInstance().e(TAG,"Return connection IP or port error," + String.format("ip:%s & port:%s", ip, port));
|
||||
WKLoggerUtils.getInstance().e(TAG, "Return connection IP or port error," + String.format("ip:%s & port:%s", ip, port));
|
||||
isReConnecting = false;
|
||||
reconnectionHandler.postDelayed(reconnectionRunnable, reconnectDelay);
|
||||
} else {
|
||||
if (lastRequestId.equals(requestId)) {
|
||||
WKConnection.this.ip = ip;
|
||||
WKConnection.this.port = port;
|
||||
WKLoggerUtils.getInstance().e(TAG,"connection address " + ip + ":" + port);
|
||||
if (connectionIsNull()) {
|
||||
executors.execute(WKConnection.this::connSocket);
|
||||
// new Thread(WKConnection.this::connSocket).start();
|
||||
}
|
||||
} else {
|
||||
if (connectionIsNull()) {
|
||||
WKLoggerUtils.getInstance().e(TAG,"The IP number requested is inconsistent, reconnecting");
|
||||
reconnectionHandler.postDelayed(reconnectionRunnable, reconnectDelay);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (lastRequestId.equals(requestId)) {
|
||||
WKConnection.this.ip = ip;
|
||||
WKConnection.this.port = port;
|
||||
if (connectionIsNull()) {
|
||||
executors.execute(WKConnection.this::connSocket);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (connectionIsNull()) {
|
||||
WKLoggerUtils.getInstance().e(TAG, "The IP number requested is inconsistent, reconnecting");
|
||||
reconnectionHandler.postDelayed(reconnectionRunnable, reconnectDelay);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void connSocket() {
|
||||
private synchronized void connSocket() {
|
||||
closeConnect();
|
||||
try {
|
||||
socketSingleID = UUID.randomUUID().toString().replace("-", "");
|
||||
@ -186,7 +181,7 @@ public class WKConnection {
|
||||
connection.setAutoflush(true);
|
||||
} catch (Exception e) {
|
||||
isReConnecting = false;
|
||||
WKLoggerUtils.getInstance().e(TAG,"connection exception:" + e.getMessage());
|
||||
WKLoggerUtils.getInstance().e(TAG, "connection exception:" + e.getMessage());
|
||||
reconnection();
|
||||
}
|
||||
}
|
||||
@ -253,7 +248,6 @@ public class WKConnection {
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
//将要发送的消息添加到队列
|
||||
@ -264,7 +258,7 @@ public class WKConnection {
|
||||
|
||||
//处理登录消息状态
|
||||
private void handleLoginStatus(short status) {
|
||||
WKLoggerUtils.getInstance().e(TAG,"connection status:" + status);
|
||||
WKLoggerUtils.getInstance().e(TAG, "connection status:" + status);
|
||||
String reason = WKConnectReason.ConnectSuccess;
|
||||
if (status == WKConnectStatus.kicked) {
|
||||
reason = WKConnectReason.ReasonAuthFail;
|
||||
@ -292,12 +286,12 @@ public class WKConnection {
|
||||
});
|
||||
}
|
||||
} else if (status == WKConnectStatus.kicked) {
|
||||
WKLoggerUtils.getInstance().e(TAG,"Received kicked message");
|
||||
WKLoggerUtils.getInstance().e(TAG, "Received kicked message");
|
||||
MessageHandler.getInstance().updateLastSendingMsgFail();
|
||||
WKIMApplication.getInstance().isCanConnect = false;
|
||||
stopAll();
|
||||
} else {
|
||||
WKLoggerUtils.getInstance().e(TAG,"parsing login returns error type:" + status);
|
||||
WKLoggerUtils.getInstance().e(TAG, "parsing login returns error type:" + status);
|
||||
stopAll();
|
||||
reconnection();
|
||||
}
|
||||
@ -319,7 +313,7 @@ public class WKConnection {
|
||||
}
|
||||
int status = MessageHandler.getInstance().sendMessage(connection, mBaseMsg);
|
||||
if (status == 0) {
|
||||
WKLoggerUtils.getInstance().e(TAG,"send message failed");
|
||||
WKLoggerUtils.getInstance().e(TAG, "send message failed");
|
||||
reconnection();
|
||||
}
|
||||
}
|
||||
@ -362,7 +356,7 @@ public class WKConnection {
|
||||
MsgDbManager.getInstance().updateMsgStatus(item.getKey(), WKSendMsgResult.send_fail);
|
||||
it.remove();
|
||||
wkSendingMsg.isCanResend = false;
|
||||
WKLoggerUtils.getInstance().e(TAG,"checkSendingMsg send message failed");
|
||||
WKLoggerUtils.getInstance().e(TAG, "checkSendingMsg send message failed");
|
||||
} else {
|
||||
long nowTime = DateUtils.getInstance().getCurrentSeconds();
|
||||
if (nowTime - wkSendingMsg.sendTime > 10) {
|
||||
@ -370,7 +364,7 @@ public class WKConnection {
|
||||
sendingMsgHashMap.put(item.getKey(), wkSendingMsg);
|
||||
wkSendingMsg.sendCount++;
|
||||
sendMessage(Objects.requireNonNull(sendingMsgHashMap.get(item.getKey())).wkSendMsg);
|
||||
WKLoggerUtils.getInstance().e(TAG,"checkSendingMsg send message failed");
|
||||
WKLoggerUtils.getInstance().e(TAG, "checkSendingMsg send message failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -502,7 +496,7 @@ public class WKConnection {
|
||||
System.gc();
|
||||
}
|
||||
|
||||
private void closeConnect() {
|
||||
private synchronized void closeConnect() {
|
||||
if (connection != null && connection.isOpen()) {
|
||||
try {
|
||||
WKLoggerUtils.getInstance().e("stop connection:" + connection.getId());
|
||||
@ -533,7 +527,7 @@ public class WKConnection {
|
||||
checkNetWorkTimer.purge();
|
||||
checkNetWorkTimer = null;
|
||||
if (TextUtils.isEmpty(ip) || port == 0) {
|
||||
WKLoggerUtils.getInstance().e(TAG,"Request for IP has timed out");
|
||||
WKLoggerUtils.getInstance().e(TAG, "Request for IP has timed out");
|
||||
isReConnecting = false;
|
||||
reconnection();
|
||||
}
|
||||
@ -542,9 +536,9 @@ public class WKConnection {
|
||||
checkNetWorkTimer.cancel();
|
||||
checkNetWorkTimer.purge();
|
||||
checkNetWorkTimer = null;
|
||||
WKLoggerUtils.getInstance().e(TAG,"Request IP countdown has been destroyed");
|
||||
WKLoggerUtils.getInstance().e(TAG, "Request IP countdown has been destroyed");
|
||||
} else {
|
||||
WKLoggerUtils.getInstance().e(TAG,"Requesting IP countdown--->" + (nowTime - requestIPTime));
|
||||
WKLoggerUtils.getInstance().e(TAG, "Requesting IP countdown--->" + (nowTime - requestIPTime));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -76,6 +76,7 @@ class WKProto {
|
||||
}
|
||||
|
||||
byte[] enConnectMsg(WKConnectMsg connectMsg) {
|
||||
CryptoUtils.getInstance().initKey();
|
||||
byte[] remainingBytes = WKTypeUtils.getInstance().getRemainingLengthByte(connectMsg.getRemainingLength());
|
||||
int totalLen = connectMsg.getTotalLen();
|
||||
WKWrite wkWrite = new WKWrite(totalLen);
|
||||
@ -89,7 +90,6 @@ class WKProto {
|
||||
wkWrite.writeString(WKIMApplication.getInstance().getToken());
|
||||
wkWrite.writeLong(connectMsg.clientTimestamp);
|
||||
wkWrite.writeString(CryptoUtils.getInstance().getPublicKey());
|
||||
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
WKLoggerUtils.getInstance().e(TAG, "enConnectMsg error");
|
||||
}
|
||||
|
@ -107,6 +107,7 @@ class WKTimers {
|
||||
}, 1000 * 7, 1000 * 7);
|
||||
}
|
||||
|
||||
boolean isForcedReconnect;
|
||||
|
||||
//开启检测网络定时器
|
||||
void startCheckNetWorkTimer() {
|
||||
@ -117,13 +118,16 @@ class WKTimers {
|
||||
public void run() {
|
||||
boolean is_have_network = WKIMApplication.getInstance().isNetworkConnected();
|
||||
if (!is_have_network) {
|
||||
isForcedReconnect = true;
|
||||
WKIM.getInstance().getConnectionManager().setConnectionStatus(WKConnectStatus.noNetwork, WKConnectReason.NoNetwork);
|
||||
WKLoggerUtils.getInstance().e("No network connection...");
|
||||
WKConnection.getInstance().checkSendingMsg();
|
||||
} else {
|
||||
//有网络
|
||||
if (WKConnection.getInstance().connectionIsNull())
|
||||
if (WKConnection.getInstance().connectionIsNull() || isForcedReconnect ) {
|
||||
WKConnection.getInstance().reconnection();
|
||||
isForcedReconnect = false;
|
||||
}
|
||||
}
|
||||
if (WKConnection.getInstance().connection == null || !WKConnection.getInstance().connection.isOpen()) {
|
||||
WKConnection.getInstance().reconnection();
|
||||
|
@ -54,19 +54,19 @@ public class WKSendMsg extends WKBaseMsg {
|
||||
}
|
||||
|
||||
public String getSendContent() {
|
||||
if (TextUtils.isEmpty(cryptoPayload)) {
|
||||
// if (TextUtils.isEmpty(cryptoPayload)) {
|
||||
cryptoPayload = CryptoUtils.getInstance().base64Encode(CryptoUtils.getInstance().aesEncrypt(payload));
|
||||
}
|
||||
// }
|
||||
return cryptoPayload;
|
||||
}
|
||||
|
||||
public String getMsgKey() {
|
||||
if (TextUtils.isEmpty(msgKey)) {
|
||||
// if (TextUtils.isEmpty(msgKey)) {
|
||||
String sendContent = getSendContent();
|
||||
String key = clientSeq + clientMsgNo + channelId + channelType + sendContent;
|
||||
byte[] msgKeyByte = CryptoUtils.getInstance().aesEncrypt(key);
|
||||
msgKey = CryptoUtils.getInstance().digestMD5(CryptoUtils.getInstance().base64Encode(msgKeyByte));
|
||||
}
|
||||
// }
|
||||
return msgKey;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user