Jaswanth Kumar
Jaswanth Kumar

Reputation: 3611

Socket.emit with callback in Android - NodeJS

I'm trying to communicate with NodeJS server using SocketIO in Server and Android. Currently using Gottox SocketIO-Java-Client Repository

NODEJS:

 socket.on('new user',function(data, callback){
    if (nicknames.indexOf(data)!=-1){
        callback(false);
    } else{
        callback(true);
        socket.nickname = data;
        nicknames.push(socket.nickname);
        updateNicknames();
    }
});

Android:

socket.emit("new user", "User01");

Now if I use the above code, callback error occurs at callback(true) as shown in the following link: https://i.sstatic.net/sKcog.png

Please suggest on how to implement callbacks in Android.

Thanks!

Upvotes: 0

Views: 3643

Answers (2)

Vishnu
Vishnu

Reputation: 41

Try this worked for me keep in mind that callback is in other thread

mSocket.emit("newuser", email, new Ack() {
    @Override
    public void call(Object... args) {
        Log.i("callback","test");
    }
});

Upvotes: 4

levi
levi

Reputation: 25141

Your Java client is not providing a callback. The client method should look like this:

socket.emit("new user", new IOAcknowledge() {
    public void ack(Object...args) {

    }
}, "User01");

Upvotes: 1

Related Questions