socketio / socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
https://socketio.github.io/socket.io-client-java/installation.html
Other
5.32k stars 972 forks source link

Events callbacks #693

Closed piranna closed 2 years ago

piranna commented 2 years ago

Javascript client events listeners support the last argument to be a callback function, so it's possible to call it to send back a reply to an event emitted by the server. This is also supported by the C++ library, but there's no reference to the same functionality when listening events in the Java client. I've reviewed the actual Java library source code, but found no clue about it.

darrachequesne commented 2 years ago

Please check the documentation here: https://socketio.github.io/socket.io-client-java/emitting_events.html#Acknowledgements

piranna commented 2 years ago

I've already check the documentation before, and that's NOT what I'm asking about.

That link you've send me is about Java client emitting an event, and including an acknowledge callback to receive a response from the server. What I'm asking about is the other way, the server emitting an event with an acknowledge, and the Java client having a callback argument in its event listener to be able to send back the response to the server. That last functionality is missing in the Java client, but it's available at least for Javascript, C++ and iOS clients, as shown in the referenced links.

Please re-open this issue, it's not solved.

darrachequesne commented 2 years ago

@piranna you are absolutely right, sorry for dismissing the issue too quickly. I've added an example with a server to client ack: https://socketio.github.io/socket.io-client-java/emitting_events.html#From_server_to_client

Server

io.on("connection", (socket) => {
  socket.emit("hello", "please acknowledge", (response) => {
    console.log(response); // prints "hi!"
  });
});

Client

// Java 7
socket.on("hello", new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        System.out.println(args[0]); // "please acknowledge"
        if (args.length > 1 && args[1] instanceof Ack) {
            ((Ack) args[1]).call("hi!");
        }
    }
});

// Java 8 and above
socket.on("hello", args -> {
    System.out.println(args[0]); // "please acknowledge"
    if (args.length > 1 && args[1] instanceof Ack) {
        ((Ack) args[1]).call("hi!");
    }
});
piranna commented 2 years ago

Ah, great, that was the info I was looking for, thank you for adding the example in the docs :-)