I am using socketio.jar to establish the connection between Client and Server.
I.e. 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
Socket.IO primarily uses the WebSocket protocol with polling as a fallback option, while providing the same interface.
The first step is to install the Java Socket.IO client with Gradle. We must remember adding the internet permission to AndroidManifest. xml . Now we can use Socket.IO on Android!
The Socket.IO library keeps an open TCP connection to the server, which may result in a high battery drain for your users.
Server API | Socket.IO.
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());
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With