1) 在断开连接模式下,我正在向服务器发送一些数据,当出现套接字错误时,我会显示消息“请检查您的互联网连接并重试”。
2) 当套接字重新连接时,它会将步骤1的数据发送到服务器(根据功能应该被丢弃)。
我不知道关闭/断开连接是否清除缓冲区,我也希望在连接可用时自动重新连接。 我正在创建Android应用程序并使用socket.io。
1) 在断开连接模式下,我正在向服务器发送一些数据,当出现套接字错误时,我会显示消息“请检查您的互联网连接并重试”。
2) 当套接字重新连接时,它会将步骤1的数据发送到服务器(根据功能应该被丢弃)。
我不知道关闭/断开连接是否清除缓冲区,我也希望在连接可用时自动重新连接。 我正在创建Android应用程序并使用socket.io。
ackMessageTimeOut = new AckMessageTimeOut(5000) {
@Override
public void call(Object... args) {
if (args != null) {
if (args[0].toString().equalsIgnoreCase("No Ack")) {
Log.d("ACK_SOCKET", "AckWithTimeOut : " + args[0].toString());
acknowledgeMessage(null);
if (socket != null) {
socket.close();
socket = null;
connectSocket(); // Custom method which creates the socket and tries to connect it.
}
} else if (args[0].toString().equalsIgnoreCase("true")) {
cancelTimer(); //cancel timer if emit ACK return true
Log.d("ACK_SOCKET", "AckWithTimeOut : " + args[0].toString());
}
}
}
};
socket.emit("sendMessage", message, ackMessageTimeOut); // You can apply it on whatever event you want.
private class AckMessageTimeOut implements Ack {
private Timer timer;
private long timeOut = 0;
private boolean called = false;
AckMessageTimeOut(long timeout_after) {
if (timeout_after <= 0)
return;
this.timeOut = timeout_after;
startTimer();
}
private void startTimer() {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
callback("No Ack");
}
}, timeOut);
}
private void resetTimer() {
if (timer != null) {
timer.cancel();
startTimer();
}
}
void cancelTimer() {
if (timer != null)
timer.cancel();
}
void callback(Object... args) {
if (called)
return;
called = true;
cancelTimer();
call(args);
}
@Override
public void call(Object... objects) {
}
}