Commit 29fde031 by pye52

优化debug模式下的界面提示语

1、重连下会留5秒的延迟让UI界面能充分显示提示语
2、现在onError不会触发切换网络及重连(避免和onClose逻辑重复)
3、增加获取当前正在尝试切换的网络状态(由debug界面获取并呈现)
parent 06652865
......@@ -117,26 +117,26 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
SCWebSocketClient.getInstance().getConnectStateEvent().observe(this, event -> {
if (debugLayoutIsNotInflate()) return;
String networkType = NetworkUtils.getNetworkType(this);
String msg;
LogUtils.d(TAG, "connect state: " + event.getState());
switch (event.getState()) {
case ConnectState.OFFLINE:
msg = String.format(getString(R.string.connect_offline), networkType);
msg = String.format(getString(R.string.connect_offline), NetworkUtils.getTryingNetworkType(this));
break;
case ConnectState.CONNECTING:
msg = String.format(getString(R.string.connect_connecting), networkType);
msg = String.format(getString(R.string.connect_connecting), NetworkUtils.getNetworkType(this));
break;
case ConnectState.CONNECTED:
msg = String.format(getString(R.string.connect_connected), event.getHost(), networkType);
msg = String.format(getString(R.string.connect_connected), event.getHost(), NetworkUtils.getNetworkType(this));
// socket链接成功后刷新wifi及eth的ip地址信息
refreshEthInfo();
refreshWifiInfo();
break;
case ConnectState.RECONNECTING:
msg = String.format(getString(R.string.connect_reconnecting), event.getHost(), networkType);
msg = String.format(getString(R.string.connect_reconnecting), event.getHost());
break;
case ConnectState.CHANGE_NETWORK:
msg = String.format(getString(R.string.connect_change_network), event.getHost(), networkType);
msg = String.format(getString(R.string.connect_change_network), NetworkUtils.getTryingNetworkType(this));
break;
default:
msg = "";
......
......@@ -206,11 +206,6 @@ public class SCWebSocketClient extends WebSocketClient {
for (SCWebSocketListener l : listener) {
l.onError(e);
}
if (forceStop) return;
switchNetwork();
stopHeartbeat();
tryReconnect();
}
@SuppressLint("ObsoleteSdkInt")
......@@ -221,7 +216,8 @@ public class SCWebSocketClient extends WebSocketClient {
}
lastSwitchTime = currentTime;
connectState.postValue(new ConnectState(ConnectState.CHANGE_NETWORK, host.toString()));
LogUtils.d(TAG, "switch network");
connectState.postValue(new ConnectState(ConnectState.CHANGE_NETWORK));
NetworkUtils.switchNetwork(callback);
}
......@@ -245,6 +241,11 @@ public class SCWebSocketClient extends WebSocketClient {
private Runnable reconnectRunnable = () -> {
LogUtils.d(TAG, "开始尝试重连");
connectState.postValue(new ConnectState(ConnectState.RECONNECTING, host.toString()));
// 增加一个延时,让UI界面有足够的时间去显示debug信息
try {
Thread.sleep(RECONNECT_DELAY);
} catch (InterruptedException ignored) {
}
for (SCWebSocketListener l : listener) {
l.onReconnect();
}
......
......@@ -151,6 +151,17 @@ public class NetworkUtils {
}
}
/**
* 获取当前正在尝试切换的网络形态
*/
public static String getTryingNetworkType(Context context) {
if (lastTryNetwork == NETWORK_ETHERNET) {
return context.getString(R.string.network_type_ethernet);
} else {
return context.getString(R.string.network_type_wifi);
}
}
public static String getNetworkType(Context context) {
if (Build.VERSION.SDK_INT >= 23) {
Network network = connectivityManager.getActiveNetwork();
......
......@@ -25,6 +25,6 @@
<string name="connect_offline">%s 未连接</string>
<string name="connect_connecting">%s 正在连接…</string>
<string name="connect_connected">服务器: %s,网络类型: %s</string>
<string name="connect_reconnecting">服务器: %s,正在重连… %s</string>
<string name="connect_reconnecting">服务器: %s,正在重连…</string>
<string name="connect_change_network">%s 正在切换…</string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment