3

1) In disconnected mode I am sending some data to server and on socket error I am displaying message "Please check your internet connection and try again".

2) When socket reconnect it is sending step 1 data to server (As per functionality it should be discarded).

I don't know close/disconnect clears buffer or not and I also want to reconnect automatically when connection is available. I am creating android app and using socket.io.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
me_astr
  • 922
  • 1
  • 14
  • 21
  • 1
    Related info here: http://stackoverflow.com/questions/32131629/socketio-stop-re-emitting-event-after-x-seconds-first-failed-attempt-to-get-a-re/32261523#32261523. – jfriend00 Dec 31 '15 at 03:31
  • Thanks for your help. I am new to java so this question may look silly. I am using socket.io.client library (chttps://github.com/socketio/socket.io-client-java) I got sendBuffer in my Socket class (it is private class variable so can't access it)(src/main/java/io/socket/client/Socket.java) private final Queue> sendBuffer = new LinkedList>(); Only public method that has sendBuffer public Emitter emit(final String event, final Object... args) { //Lot of code (this code is modifying some priavate class variable, so cant overwrite. – me_astr Jan 02 '16 at 22:52
  • if (Socket.this.connected) { Socket.this.packet(packet); } else { Socket.this.sendBuffer.add(packet); } Can you pleae suggest anything to clear sendBuffer in this case ? I also tried to clone this repostitory and than add it as module but it has lot of other depenceies as well, so getting lot of complie error. – me_astr Jan 02 '16 at 22:52
  • 1
    If you're programming in Java, I can't help further. Your question did not tag Java (I just added that tag to your question) so I wrongly assumed you were in Javascript. You will likely have to study the code for the Java implementation to figure out what change you could make (like what I did in the above reference for Javascript). – jfriend00 Jan 02 '16 at 23:20

1 Answers1

1

Here is a way out to perform it which worked for me, it may be a little tricky and yes the code here is a combination of several answers which I found on StackOverflow.

The trick I used is to close the socket, make it NULL and then recreate and try to connect it. It helps to clear the buffer and reconnect in a smooth way.

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.

AckMessageTimeOut class is as below:

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) {

        }
    }
Harpreet
  • 2,990
  • 3
  • 38
  • 52