mirror of
https://github.com/WuKongIM/WuKongIMAndroidSDK
synced 2025-06-07 10:01:02 +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
|
* 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() {
|
||||||
|
|
||||||
|
@ -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,13 +133,8 @@ 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();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -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)) {
|
}
|
||||||
WKConnection.this.ip = ip;
|
if (lastRequestId.equals(requestId)) {
|
||||||
WKConnection.this.port = port;
|
WKConnection.this.ip = ip;
|
||||||
WKLoggerUtils.getInstance().e(TAG,"connection address " + ip + ":" + port);
|
WKConnection.this.port = port;
|
||||||
if (connectionIsNull()) {
|
if (connectionIsNull()) {
|
||||||
executors.execute(WKConnection.this::connSocket);
|
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 (connectionIsNull()) {
|
||||||
|
WKLoggerUtils.getInstance().e(TAG, "The IP number requested is inconsistent, reconnecting");
|
||||||
|
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("-", "");
|
||||||
@ -186,7 +181,7 @@ public class WKConnection {
|
|||||||
connection.setAutoflush(true);
|
connection.setAutoflush(true);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
isReConnecting = false;
|
isReConnecting = false;
|
||||||
WKLoggerUtils.getInstance().e(TAG,"connection exception:" + e.getMessage());
|
WKLoggerUtils.getInstance().e(TAG, "connection exception:" + e.getMessage());
|
||||||
reconnection();
|
reconnection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -253,7 +248,6 @@ public class WKConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//将要发送的消息添加到队列
|
//将要发送的消息添加到队列
|
||||||
@ -264,7 +258,7 @@ public class WKConnection {
|
|||||||
|
|
||||||
//处理登录消息状态
|
//处理登录消息状态
|
||||||
private void handleLoginStatus(short status) {
|
private void handleLoginStatus(short status) {
|
||||||
WKLoggerUtils.getInstance().e(TAG,"connection status:" + status);
|
WKLoggerUtils.getInstance().e(TAG, "connection status:" + status);
|
||||||
String reason = WKConnectReason.ConnectSuccess;
|
String reason = WKConnectReason.ConnectSuccess;
|
||||||
if (status == WKConnectStatus.kicked) {
|
if (status == WKConnectStatus.kicked) {
|
||||||
reason = WKConnectReason.ReasonAuthFail;
|
reason = WKConnectReason.ReasonAuthFail;
|
||||||
@ -292,12 +286,12 @@ public class WKConnection {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (status == WKConnectStatus.kicked) {
|
} else if (status == WKConnectStatus.kicked) {
|
||||||
WKLoggerUtils.getInstance().e(TAG,"Received kicked message");
|
WKLoggerUtils.getInstance().e(TAG, "Received kicked message");
|
||||||
MessageHandler.getInstance().updateLastSendingMsgFail();
|
MessageHandler.getInstance().updateLastSendingMsgFail();
|
||||||
WKIMApplication.getInstance().isCanConnect = false;
|
WKIMApplication.getInstance().isCanConnect = false;
|
||||||
stopAll();
|
stopAll();
|
||||||
} else {
|
} else {
|
||||||
WKLoggerUtils.getInstance().e(TAG,"parsing login returns error type:" + status);
|
WKLoggerUtils.getInstance().e(TAG, "parsing login returns error type:" + status);
|
||||||
stopAll();
|
stopAll();
|
||||||
reconnection();
|
reconnection();
|
||||||
}
|
}
|
||||||
@ -319,7 +313,7 @@ public class WKConnection {
|
|||||||
}
|
}
|
||||||
int status = MessageHandler.getInstance().sendMessage(connection, mBaseMsg);
|
int status = MessageHandler.getInstance().sendMessage(connection, mBaseMsg);
|
||||||
if (status == 0) {
|
if (status == 0) {
|
||||||
WKLoggerUtils.getInstance().e(TAG,"send message failed");
|
WKLoggerUtils.getInstance().e(TAG, "send message failed");
|
||||||
reconnection();
|
reconnection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -362,7 +356,7 @@ public class WKConnection {
|
|||||||
MsgDbManager.getInstance().updateMsgStatus(item.getKey(), WKSendMsgResult.send_fail);
|
MsgDbManager.getInstance().updateMsgStatus(item.getKey(), WKSendMsgResult.send_fail);
|
||||||
it.remove();
|
it.remove();
|
||||||
wkSendingMsg.isCanResend = false;
|
wkSendingMsg.isCanResend = false;
|
||||||
WKLoggerUtils.getInstance().e(TAG,"checkSendingMsg send message failed");
|
WKLoggerUtils.getInstance().e(TAG, "checkSendingMsg send message failed");
|
||||||
} else {
|
} else {
|
||||||
long nowTime = DateUtils.getInstance().getCurrentSeconds();
|
long nowTime = DateUtils.getInstance().getCurrentSeconds();
|
||||||
if (nowTime - wkSendingMsg.sendTime > 10) {
|
if (nowTime - wkSendingMsg.sendTime > 10) {
|
||||||
@ -370,7 +364,7 @@ public class WKConnection {
|
|||||||
sendingMsgHashMap.put(item.getKey(), wkSendingMsg);
|
sendingMsgHashMap.put(item.getKey(), wkSendingMsg);
|
||||||
wkSendingMsg.sendCount++;
|
wkSendingMsg.sendCount++;
|
||||||
sendMessage(Objects.requireNonNull(sendingMsgHashMap.get(item.getKey())).wkSendMsg);
|
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();
|
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());
|
||||||
@ -533,7 +527,7 @@ public class WKConnection {
|
|||||||
checkNetWorkTimer.purge();
|
checkNetWorkTimer.purge();
|
||||||
checkNetWorkTimer = null;
|
checkNetWorkTimer = null;
|
||||||
if (TextUtils.isEmpty(ip) || port == 0) {
|
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;
|
isReConnecting = false;
|
||||||
reconnection();
|
reconnection();
|
||||||
}
|
}
|
||||||
@ -542,9 +536,9 @@ public class WKConnection {
|
|||||||
checkNetWorkTimer.cancel();
|
checkNetWorkTimer.cancel();
|
||||||
checkNetWorkTimer.purge();
|
checkNetWorkTimer.purge();
|
||||||
checkNetWorkTimer = null;
|
checkNetWorkTimer = null;
|
||||||
WKLoggerUtils.getInstance().e(TAG,"Request IP countdown has been destroyed");
|
WKLoggerUtils.getInstance().e(TAG, "Request IP countdown has been destroyed");
|
||||||
} else {
|
} 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) {
|
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");
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user