[change]优化ws关闭逻辑
This commit is contained in:
@@ -103,6 +103,7 @@ public class ReceiveTimeoutManager {
|
||||
public void run() {
|
||||
long difference = timeoutPeriod - ((long) lastReceiveTime * 1000);
|
||||
timeoutPeriod = ((long) lastReceiveTime * 1000) + timeout;
|
||||
CupidLogUtils.e(TAG, "最后一次接收时间=" + lastReceiveTime + "秒");
|
||||
CupidLogUtils.e(TAG, "最后一次接收工控机数据时间与当前时间差=" + (difference / 1000.0) + "秒");
|
||||
if (difference >= timeout) {
|
||||
if (listener != null) {
|
||||
|
||||
@@ -52,7 +52,7 @@ public class FpgaSocket implements IWebSocket {
|
||||
|
||||
private EchoWebSocketListener listener;
|
||||
private IWebSocketConnectListener mWebSocketConnectListener;
|
||||
private Request request;
|
||||
|
||||
|
||||
private OkHttpClient.Builder okBuilder;
|
||||
|
||||
@@ -94,7 +94,7 @@ public class FpgaSocket implements IWebSocket {
|
||||
@Override
|
||||
public void onTimeout(double time) {
|
||||
receiveTimeoutReason = "心跳超时(" + time + "秒)";
|
||||
onPassiveClose(receiveTimeoutReason);
|
||||
onPassiveClose(1001, receiveTimeoutReason);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -119,10 +119,9 @@ public class FpgaSocket implements IWebSocket {
|
||||
if (client != null && mWebSocket == null) {
|
||||
if (mWebSocketConnectListener != null)
|
||||
mWebSocketConnectListener.onConnecting(msg);
|
||||
if (request == null)
|
||||
request = new Request.Builder()
|
||||
.url(wsHost)
|
||||
.build();
|
||||
Request request = new Request.Builder()
|
||||
.url(wsHost)
|
||||
.build();
|
||||
mWebSocket = client.newWebSocket(request, listener);
|
||||
}
|
||||
}
|
||||
@@ -147,7 +146,6 @@ public class FpgaSocket implements IWebSocket {
|
||||
|
||||
@Override
|
||||
public void closeWebSocket() {
|
||||
CupidLogUtils.i(TAG, "WebSocket 主动断开连接");
|
||||
if (AdasChannel.isUseQueue) {
|
||||
WebSocketQueueManager.getInstance().release();
|
||||
WSByteQueueManager.getInstance().release();
|
||||
@@ -161,7 +159,6 @@ public class FpgaSocket implements IWebSocket {
|
||||
}
|
||||
listener = null;
|
||||
client = null;
|
||||
request = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -274,26 +271,36 @@ public class FpgaSocket implements IWebSocket {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 此回调服务端发起关闭
|
||||
*/
|
||||
@Override
|
||||
public void onClosing(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
|
||||
super.onClosing(webSocket, code, reason);
|
||||
CupidLogUtils.e(TAG, code + "WebSocket onClosing= " + reason);
|
||||
String msg = "Code=" + code + " ";
|
||||
String msg = "Closing Code=" + code + " reason=";
|
||||
if (TextUtils.isEmpty(reason))
|
||||
reason = "未知原因";
|
||||
reason = "服务端关闭";
|
||||
msg += reason;
|
||||
onPassiveClose("Closing " + msg);
|
||||
CupidLogUtils.e(TAG, msg);
|
||||
int clientCloseCode = 1000;
|
||||
if (code != 1000) {
|
||||
clientCloseCode = 1001;
|
||||
}
|
||||
onPassiveClose(clientCloseCode, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 此回调表示两端正常关闭完成
|
||||
*/
|
||||
@Override
|
||||
public void onClosed(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
|
||||
super.onClosed(webSocket, code, reason);
|
||||
CupidLogUtils.e(TAG, code + "WebSocket onClosed= " + reason);
|
||||
String msg = "Code=" + code + " ";
|
||||
if (TextUtils.isEmpty(reason))
|
||||
reason = "未知原因";
|
||||
msg += reason;
|
||||
onPassiveClose("Closed " + msg);
|
||||
CupidLogUtils.e(TAG, code + " Closed reason= " + reason);
|
||||
// String msg = "Code=" + code + " ";
|
||||
// if (TextUtils.isEmpty(reason))
|
||||
// reason = "未知原因";
|
||||
// msg += reason;
|
||||
// onPassiveClose("Closed " + msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -338,10 +345,10 @@ public class FpgaSocket implements IWebSocket {
|
||||
*
|
||||
* @param reason
|
||||
*/
|
||||
private void onPassiveClose(String reason) {
|
||||
private void onPassiveClose(int code, String reason) {
|
||||
isPassiveClose.set(true);
|
||||
if (mWebSocket != null) {
|
||||
close(false, 1001);
|
||||
close(false, code);
|
||||
}
|
||||
onConnectFailed(reason);
|
||||
}
|
||||
@@ -351,9 +358,15 @@ public class FpgaSocket implements IWebSocket {
|
||||
* @param code code
|
||||
*/
|
||||
private void close(boolean isInitiative, int code) {
|
||||
boolean isClose = mWebSocket.close(code, null);
|
||||
String closeReason = null;
|
||||
if (isInitiative) {
|
||||
closeReason = "用户主动关闭";
|
||||
}
|
||||
boolean isClose = mWebSocket.close(code, closeReason);
|
||||
CupidLogUtils.i(TAG, "WebSocket " + (isInitiative ? "主动" : "被动") + "断开连接是否成功= " + isClose);
|
||||
mWebSocket.cancel();
|
||||
if (!isInitiative) {
|
||||
mWebSocket.cancel();
|
||||
}
|
||||
mWebSocket = null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user