Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emit with ack SocketIO with iOS

I'm trying to make an emit with a callback, but it seems like the server doesn't recognize the call back function as a function. I have looked up the documentation, and to my knowledge the code should work.

This is the error I'm getting: TypeError: callBack is not a function

Here is the client code:

socket?.emitWithAck("connectUsername", username).timingOut(after: 2, callback: { (data) in
            
        print(data)
    })

And server code:    

socket.on("connectUsername", function(username, callBack)
    {
        //Do stuff with username here...

        var id = socket.id
        callBack(id);
     });

Any help is appreciated.

like image 310
Elhoej Avatar asked Jun 11 '18 08:06

Elhoej


People also ask

Does Socket.IO work on iOS?

Socket.IO is a framework that makes it easy to implement Socket and the available for iOS, Android, back-end and front-end.

What does Socket.IO emit do?

emit() to send a message to all the connected clients. This code will notify when a user connects to the server.

Does Socket.IO work on mobile?

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!

What protocol does Socket.IO use?

Socket.IO primarily uses the WebSocket protocol with polling as a fallback option, while providing the same interface. Although it can be used simply as a wrapper for WebSockets, it provides many more features, including broadcasting to multiple sockets, storing data associated with each client, and asynchronous I/O.


2 Answers

as @kiddosrails mention problem is the timeout 2 seconds is less time to execute, you can set timeout 0 which refer as not time out. Hope this answer will help you.

     socket.emitWithAck("connectUsername", username).timingOut(after: 0) {data in
      print(data)    
    }
like image 74
Sumit Meena Avatar answered Sep 30 '22 04:09

Sumit Meena


I don't see callback as a parameter of timingOut in documentation. Following should work:

socket.emitWithAck("connectUsername", username).timingOut(after: 20) { data in 
  print(data)
}

If above does not work, check what exactly is callBack by changing it to following:

socket.on("connectUsername", function(username, callBack)
    {
        //Do stuff with username here...

        var id = socket.id
        console.log(callBack.toString());
     });
like image 26
kiddorails Avatar answered Sep 30 '22 05:09

kiddorails