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 * 5/20/21 5:25 PM
*/ */
public class WKIM { public class WKIM {
private final String Version = "V1.1.9"; private final String Version = "V1.2.3";
private WKIM() { private WKIM() {

View File

@ -62,6 +62,7 @@ import java.util.concurrent.TimeUnit;
*/ */
public class WKConnection { public class WKConnection {
private final String TAG = "WKConnection"; private final String TAG = "WKConnection";
private WKConnection() { private WKConnection() {
} }
@ -132,11 +133,6 @@ public class WKConnection {
} }
private synchronized void getIPAndPort() { private synchronized void getIPAndPort() {
if (!WKIMApplication.getInstance().isNetworkConnected()) {
isReConnecting = false;
reconnectionHandler.postDelayed(reconnectionRunnable, reconnectDelay);
return;
}
if (!WKIMApplication.getInstance().isCanConnect) { 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(); stopAll();
@ -147,30 +143,29 @@ public class WKConnection {
startRequestIPTimer(); startRequestIPTimer();
lastRequestId = UUID.randomUUID().toString().replace("-", ""); lastRequestId = UUID.randomUUID().toString().replace("-", "");
ConnectionManager.getInstance().getIpAndPort(lastRequestId, (requestId, ip, port) -> { ConnectionManager.getInstance().getIpAndPort(lastRequestId, (requestId, ip, port) -> {
WKLoggerUtils.getInstance().e(TAG, "connection address " + ip + ":" + port);
if (TextUtils.isEmpty(ip) || port == 0) { 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; isReConnecting = false;
reconnectionHandler.postDelayed(reconnectionRunnable, reconnectDelay); reconnectionHandler.postDelayed(reconnectionRunnable, reconnectDelay);
} else { return;
}
if (lastRequestId.equals(requestId)) { if (lastRequestId.equals(requestId)) {
WKConnection.this.ip = ip; WKConnection.this.ip = ip;
WKConnection.this.port = port; WKConnection.this.port = port;
WKLoggerUtils.getInstance().e(TAG,"connection address " + ip + ":" + port);
if (connectionIsNull()) { if (connectionIsNull()) {
executors.execute(WKConnection.this::connSocket); executors.execute(WKConnection.this::connSocket);
// new Thread(WKConnection.this::connSocket).start();
} }
} else { return;
}
if (connectionIsNull()) { if (connectionIsNull()) {
WKLoggerUtils.getInstance().e(TAG, "The IP number requested is inconsistent, reconnecting"); WKLoggerUtils.getInstance().e(TAG, "The IP number requested is inconsistent, reconnecting");
reconnectionHandler.postDelayed(reconnectionRunnable, reconnectDelay); reconnectionHandler.postDelayed(reconnectionRunnable, reconnectDelay);
} }
}
}
}); });
} }
private void connSocket() { private synchronized void connSocket() {
closeConnect(); closeConnect();
try { try {
socketSingleID = UUID.randomUUID().toString().replace("-", ""); socketSingleID = UUID.randomUUID().toString().replace("-", "");
@ -253,7 +248,6 @@ public class WKConnection {
} }
} }
}).start(); }).start();
} }
//将要发送的消息添加到队列 //将要发送的消息添加到队列
@ -502,7 +496,7 @@ public class WKConnection {
System.gc(); System.gc();
} }
private void closeConnect() { private synchronized void closeConnect() {
if (connection != null && connection.isOpen()) { if (connection != null && connection.isOpen()) {
try { try {
WKLoggerUtils.getInstance().e("stop connection:" + connection.getId()); WKLoggerUtils.getInstance().e("stop connection:" + connection.getId());

View File

@ -76,6 +76,7 @@ class WKProto {
} }
byte[] enConnectMsg(WKConnectMsg connectMsg) { byte[] enConnectMsg(WKConnectMsg connectMsg) {
CryptoUtils.getInstance().initKey();
byte[] remainingBytes = WKTypeUtils.getInstance().getRemainingLengthByte(connectMsg.getRemainingLength()); byte[] remainingBytes = WKTypeUtils.getInstance().getRemainingLengthByte(connectMsg.getRemainingLength());
int totalLen = connectMsg.getTotalLen(); int totalLen = connectMsg.getTotalLen();
WKWrite wkWrite = new WKWrite(totalLen); WKWrite wkWrite = new WKWrite(totalLen);
@ -89,7 +90,6 @@ class WKProto {
wkWrite.writeString(WKIMApplication.getInstance().getToken()); wkWrite.writeString(WKIMApplication.getInstance().getToken());
wkWrite.writeLong(connectMsg.clientTimestamp); wkWrite.writeLong(connectMsg.clientTimestamp);
wkWrite.writeString(CryptoUtils.getInstance().getPublicKey()); wkWrite.writeString(CryptoUtils.getInstance().getPublicKey());
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
WKLoggerUtils.getInstance().e(TAG, "enConnectMsg error"); WKLoggerUtils.getInstance().e(TAG, "enConnectMsg error");
} }

View File

@ -107,6 +107,7 @@ class WKTimers {
}, 1000 * 7, 1000 * 7); }, 1000 * 7, 1000 * 7);
} }
boolean isForcedReconnect;
//开启检测网络定时器 //开启检测网络定时器
void startCheckNetWorkTimer() { void startCheckNetWorkTimer() {
@ -117,13 +118,16 @@ class WKTimers {
public void run() { public void run() {
boolean is_have_network = WKIMApplication.getInstance().isNetworkConnected(); boolean is_have_network = WKIMApplication.getInstance().isNetworkConnected();
if (!is_have_network) { if (!is_have_network) {
isForcedReconnect = true;
WKIM.getInstance().getConnectionManager().setConnectionStatus(WKConnectStatus.noNetwork, WKConnectReason.NoNetwork); WKIM.getInstance().getConnectionManager().setConnectionStatus(WKConnectStatus.noNetwork, WKConnectReason.NoNetwork);
WKLoggerUtils.getInstance().e("No network connection..."); WKLoggerUtils.getInstance().e("No network connection...");
WKConnection.getInstance().checkSendingMsg(); WKConnection.getInstance().checkSendingMsg();
} else { } else {
//有网络 //有网络
if (WKConnection.getInstance().connectionIsNull()) if (WKConnection.getInstance().connectionIsNull() || isForcedReconnect ) {
WKConnection.getInstance().reconnection(); WKConnection.getInstance().reconnection();
isForcedReconnect = false;
}
} }
if (WKConnection.getInstance().connection == null || !WKConnection.getInstance().connection.isOpen()) { if (WKConnection.getInstance().connection == null || !WKConnection.getInstance().connection.isOpen()) {
WKConnection.getInstance().reconnection(); WKConnection.getInstance().reconnection();

View File

@ -54,19 +54,19 @@ public class WKSendMsg extends WKBaseMsg {
} }
public String getSendContent() { public String getSendContent() {
if (TextUtils.isEmpty(cryptoPayload)) { // if (TextUtils.isEmpty(cryptoPayload)) {
cryptoPayload = CryptoUtils.getInstance().base64Encode(CryptoUtils.getInstance().aesEncrypt(payload)); cryptoPayload = CryptoUtils.getInstance().base64Encode(CryptoUtils.getInstance().aesEncrypt(payload));
} // }
return cryptoPayload; return cryptoPayload;
} }
public String getMsgKey() { public String getMsgKey() {
if (TextUtils.isEmpty(msgKey)) { // if (TextUtils.isEmpty(msgKey)) {
String sendContent = getSendContent(); String sendContent = getSendContent();
String key = clientSeq + clientMsgNo + channelId + channelType + sendContent; String key = clientSeq + clientMsgNo + channelId + channelType + sendContent;
byte[] msgKeyByte = CryptoUtils.getInstance().aesEncrypt(key); byte[] msgKeyByte = CryptoUtils.getInstance().aesEncrypt(key);
msgKey = CryptoUtils.getInstance().digestMD5(CryptoUtils.getInstance().base64Encode(msgKeyByte)); msgKey = CryptoUtils.getInstance().digestMD5(CryptoUtils.getInstance().base64Encode(msgKeyByte));
} // }
return msgKey; return msgKey;
} }