Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use callback on a socket.io CLIENT?

I want to callback the server to ack that my client has received a call. In the opposite direction, this is heavily documented and working fine, but how can I ack from the client?

NB: I am using socket-io-java-client https://github.com/socketio/socket.io-client-java on Android, not JavaScript, but it shouldn't matter. Well, maybe it does.

    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 with the above mentioned library.

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

Has anybody done this or knows how to achieve that?

like image 435
Oliver Hausler Avatar asked Mar 15 '23 13:03

Oliver Hausler


1 Answers

I overlooked this. There is an example posted here:

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();
  }
});

[Credits go to Alex who just skyped me the URL.]

like image 137
Oliver Hausler Avatar answered Mar 24 '23 02:03

Oliver Hausler