Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOAcknowledge method is NOT working for SocketIO in Android?

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

like image 423
Himanshu Mori Avatar asked Mar 01 '16 10:03

Himanshu Mori


People also ask

What protocol does Socket.IO use?

Socket.IO primarily uses the WebSocket protocol with polling as a fallback option, while providing the same interface.

Can we use Socket.IO in Android?

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!

Is Socket.IO a TCP?

The Socket.IO library keeps an open TCP connection to the server, which may result in a high battery drain for your users.

Is Socket.IO an API?

Server API | Socket.IO.


1 Answers

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());
    }
});
like image 195
romtsn Avatar answered Sep 30 '22 01:09

romtsn