简体   繁体   中英

IOAcknowledge method is NOT working for SocketIO in Android?

I am using socketio.jar to establish the connection between Client and Server .

Ie from my Android device(Client) to a Node server.

As I am successfully able to connect, send and receive messages to that server.

The problem is why I am NOT getting any Acknowledgement from socket after emitting message to the server. There is a callBack Interface IOAcknowledge as parameter, that never works/invokes for me.

  socket.emit( "sendMessage", new IOAcknowledge() { @Override public void ack(Object... arg0) { System.out.println("sendMessage IOAcknowledge" + arg0.toString()); } }, "Hi!! how are you"); 

Does anyone know the solution when or how that IOAcknowledge will work?

EDIT : Docs link of socket library which i am using.

Official and Github

It seems, that you forgot to invoke the callback on the server-side code:

var io = require('socket.io')(80);
io.on('connection', function (socket) {
   socket.on('sendMessage', function (data, callback) {
       console.log('Message received:', data); 
       callback('Message successfully delivered to server!');
   });
});

For more information check this thread or docs

EDIT:

The problem also is that Ack implementation should come as last parameter of emit function, so your Java code should look like this:

socket.emit("sendMessage", "Hi!! how are you", new Ack() {
    @Override
    public void call(Object... args) {
        System.out.println("sendMessage IOAcknowledge" + args.toString());
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM