Android socket.IO中的发射或确认超时处理?

Android socket.IO中的发射或确认超时处理?,android,socket.io,Android,Socket.io,使用带有回调的socket.io发出的消息时,如果套接字在应答之前断开连接(或根本不应答),则回调函数将永远挂起。在其他情况下,网络连接低,发出套接字,但如果发出成功,则不会回调 在这些情况下,我想在emit回调中实现超时,但ACK消息没有超时 这里是我的插座发出代码,带有ACK JSONObject obj = new JSONObject(); try { obj.put("device_id", deviceVO.getDeviceId());

使用带有回调的
socket.io
发出的消息时,如果套接字在应答之前断开连接(或根本不应答),则回调函数将永远挂起。在其他情况下,网络连接低,发出套接字,但如果发出成功,则不会回调

在这些情况下,我想在emit回调中实现超时,但ACK消息没有超时

这里是我的插座发出代码,带有
ACK

    JSONObject obj = new JSONObject();
    try {

        obj.put("device_id", deviceVO.getDeviceId());
        obj.put("device_status", deviceVO.getOldStatus());

    } catch (JSONException e) {
        e.printStackTrace();
    }

   mSocket.emit("socketChangeDevice", obj, new Ack() {
            @Override
            public void call(Object... args) {
                if(args!=null){
                    Ack ack = (Ack) args[args.length - 1];
                    ack.call();
                    Log.d("ACK_SOCKET","isAck : "+ ack);
                }
            }
        });

有没有更好的方法在客户端断开连接时返回失败的回调?我需要手动实现超时?

使用套接字发出超时确认

My
AckWithTimeOut
自定义超时类与机具
Ack
接口

public class AckWithTimeOut implements Ack {

private Timer timer;
private long timeOut = 0;
private boolean called = false;

public AckWithTimeOut() {
}

public AckWithTimeOut(long timeout_after) {
    if (timeout_after <= 0)
        return;
    this.timeOut = timeout_after;
    startTimer();
}

public void startTimer() {
    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            callback("No Ack");
        }
    }, timeOut);
}

public void resetTimer() {
    if (timer != null) {
        timer.cancel();
        startTimer();
    }
}

public void cancelTimer() {
    if (timer != null)
        timer.cancel();
}

void callback(Object... args) {
    if (called) return;
    called = true;
    cancelTimer();
    call(args);
}

@Override
public void call(Object... args) {

}
}
 mSocket.emit("socketChangeDeviceAck", obj, new AckWithTimeOut(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());
                    }else if(args[0].toString().equalsIgnoreCase("true")){
                       cancelTimer(); //cancel timer if emit ACK return true
                       Log.d("ACK_SOCKET","AckWithTimeOut : "+ args[0].toString());
                    }
                }
            }
        });