Commit 39176743 authored by sheteng's avatar sheteng

链接重新链接发消息逻辑

parent bf1b3988
...@@ -50,7 +50,7 @@ public class LoginViewModel extends ViewModel { ...@@ -50,7 +50,7 @@ public class LoginViewModel extends ViewModel {
public void onSocketDisconnection(int code) { public void onSocketDisconnection(int code) {
System.out.println("onSocketDisconnection"); System.out.println("onSocketDisconnection");
//重连 //重连
login(context, appKey, appsecret, username, userId, ip, port); // login(context, appKey, appsecret, username, userId, ip, port);
} }
}); });
......
...@@ -146,10 +146,12 @@ public class NotificationsFragment extends Fragment { ...@@ -146,10 +146,12 @@ public class NotificationsFragment extends Fragment {
public void onSuccess(Long aLong) { public void onSuccess(Long aLong) {
//获取剪贴板管理器: //获取剪贴板管理器:
loginViewModel.addChannelId(aLong); loginViewModel.addChannelId(aLong);
ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); if (getContext() != null) {
ClipData mClipData = ClipData.newPlainText("Label", aLong.toString()); ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
cm.setPrimaryClip(mClipData); ClipData mClipData = ClipData.newPlainText("Label", aLong.toString());
ToastUtil.show(getContext(), "create success,id copy success"); cm.setPrimaryClip(mClipData);
ToastUtil.show(getContext(), "create success,id copy success");
}
} }
@Override @Override
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="Username" android:hint="Username"
android:text="19" android:text="20"
android:inputType="textEmailAddress" android:inputType="textEmailAddress"
android:selectAllOnFocus="true" android:selectAllOnFocus="true"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:hint="UserId" android:hint="UserId"
android:text="19" android:text="20"
android:imeActionLabel="@string/action_sign_in_short" android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionDone" android:imeOptions="actionDone"
android:selectAllOnFocus="true" android:selectAllOnFocus="true"
......
...@@ -56,7 +56,7 @@ public class CCIMClient { ...@@ -56,7 +56,7 @@ public class CCIMClient {
private Long lastSendTimemillis = 0L; //上次发送消息时间,用于时间间隔 private Long lastSendTimemillis = 0L; //上次发送消息时间,用于时间间隔
private int maxSendMsgInterval = 0; private int maxSendMsgInterval = 0;
private int maxAudioDuration = 0; private int maxAudioDuration = 0;
private int minAudioDuration = 500; private final int minAudioDuration = 500;
private String downloadPrefix; private String downloadPrefix;
private String cachePath; private String cachePath;
...@@ -115,13 +115,13 @@ public class CCIMClient { ...@@ -115,13 +115,13 @@ public class CCIMClient {
@Override @Override
public void onSocketReConnection() { public void onSocketReConnection() {
isConnected = false; // isConnected = false;
} }
@Override @Override
public void onSocketDisconnection(int code) { public void onSocketDisconnection(int code) {
isConnected = false; isConnected = false;
logger.info("disconnection"); logger.info("disconnection " + code);
resultCallback.onSocketDisconnection(code); resultCallback.onSocketDisconnection(code);
} }
......
...@@ -9,7 +9,6 @@ import com.ccwangluo.ccim.CCIMClient; ...@@ -9,7 +9,6 @@ import com.ccwangluo.ccim.CCIMClient;
import com.ccwangluo.ccim.listener.MessageListener; import com.ccwangluo.ccim.listener.MessageListener;
import com.ccwangluo.ccim.util.ErrorCode; import com.ccwangluo.ccim.util.ErrorCode;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -41,8 +40,6 @@ public class NettyChatClient implements SocketClient { ...@@ -41,8 +40,6 @@ public class NettyChatClient implements SocketClient {
private Bootstrap bootstrap; private Bootstrap bootstrap;
private Context context; private Context context;
EventLoopGroup group = new NioEventLoopGroup(); EventLoopGroup group = new NioEventLoopGroup();
private ExecutorService executor = Executors.newSingleThreadExecutor();
ScheduledExecutorService timerExcutor; ScheduledExecutorService timerExcutor;
...@@ -76,21 +73,18 @@ public class NettyChatClient implements SocketClient { ...@@ -76,21 +73,18 @@ public class NettyChatClient implements SocketClient {
@Override @Override
public void connect(MessageListener msglistner) { public void connect(MessageListener msglistner) {
this.msglitener = msglistner; this.msglitener = msglistner;
connect(); connectSync();
} }
private void connectSync() {
private void connect() { try {
executor.execute(() -> { channelFuture = bootstrap.connect(ip, port).sync();
try { } catch (Exception e) {
channelFuture = bootstrap.connect(ip, port).sync(); e.printStackTrace();
} catch (Exception e) { if (msglitener != null) {
e.printStackTrace(); msglitener.onServerDisConnect(ErrorCode.NET_ERROR);
if (msglitener != null){
msglitener.onServerDisConnect(ErrorCode.NET_ERROR);
}
} }
}); }
} }
...@@ -114,12 +108,11 @@ public class NettyChatClient implements SocketClient { ...@@ -114,12 +108,11 @@ public class NettyChatClient implements SocketClient {
if (channelFuture == null) { if (channelFuture == null) {
return; return;
} }
executor.execute(() -> { if (!channelFuture.channel().isActive()) {
connectSync();
} else {
channelFuture.channel().writeAndFlush(msg); channelFuture.channel().writeAndFlush(msg);
if (!channelFuture.channel().isActive()) { }
connect();
}
});
} }
...@@ -138,7 +131,7 @@ public class NettyChatClient implements SocketClient { ...@@ -138,7 +131,7 @@ public class NettyChatClient implements SocketClient {
if (channelFuture != null && channelFuture.channel().isActive()) { if (channelFuture != null && channelFuture.channel().isActive()) {
return true; return true;
} }
connect(); connectSync();
return false; return false;
} }
...@@ -158,7 +151,7 @@ public class NettyChatClient implements SocketClient { ...@@ -158,7 +151,7 @@ public class NettyChatClient implements SocketClient {
} else { } else {
try { try {
logger.info("reconnect"); logger.info("reconnect");
connect(); connectSync();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
......
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 to comment