Java – How do I use callbacks on socket.io clients?

How do I use callbacks on socket.io clients?… here is a solution to the problem.

How do I use callbacks on socket.io clients?

I want to call back to the server to confirm that my customer has received the call. In the opposite direction, this is heavily documented and working fine, but how do I confirm from the client?

Note: I’m using socket-io-java-client https://github.com/socketio/socket.io-client-java on Android, not JavaScript, but it doesn’t matter. Well, maybe so.

    socket.on("onMessage", new Emitter.Listener() {
                @Override public void call(Object... args) {
                     I have the args here and the last argument is a callback,
                     but what do I need to do to callback the server using that arg?
                }
            })

In Swift it looks like this, but I can’t figure out how to do this in Java using the above library.

    socket.on("onMessage") { data, ack in
        ack? ()
    }

Has anyone done this or knows how to implement that?

Solution

I overlooked this. Here’s an example:

https://github.com/socketio/socket.io-client-java

// ack from client to server
socket.on("foo", new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    Ack ack = (Ack) args[args.length - 1];
    ack.call();
  }
});

[Thanks to Alex, he just sent me the URL via Skype.] ]

Related Problems and Solutions