fix:Optimize connections

This commit is contained in:
SL 2024-10-20 22:11:53 +08:00
parent dc2b1a9913
commit 8583c38317
5 changed files with 39 additions and 41 deletions

View File

@ -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() {

View File

@ -62,6 +62,7 @@ import java.util.concurrent.TimeUnit;
*/
public class WKConnection {
private final String TAG = "WKConnection";
private WKConnection() {
}
@ -132,11 +133,6 @@ 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");
stopAll();
@ -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));
isReConnecting = false;
reconnectionHandler.postDelayed(reconnectionRunnable, reconnectDelay);
} else {
return;
}
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 {
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("-", "");
@ -253,7 +248,6 @@ public class WKConnection {
}
}
}).start();
}
//将要发送的消息添加到队列
@ -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());

View File

@ -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");
}

View File

@ -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();

View File

@ -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;
}