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.
Socket.IO is a framework that makes it easy to implement Socket and the available for iOS, Android, back-end and front-end.
emit() to send a message to all the connected clients. This code will notify when a user connects to the server.
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!
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.
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)
}
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());
});
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