nuclearace / Socket.IO-Client-Swift

socket.io-client for Swift
Other
361 stars 53 forks source link

How to emitWithAck? #108

Open kylebshr opened 8 years ago

kylebshr commented 8 years ago

Hello,

Just started using socketio in a swift game I'm developing. We'd like to ack back and forth on every event but I'm unsure of how to do this. There's an emitWithAck function, but it says to add ack handlers to SocketAckHandler, which I can't find anywhere else in the code.

I was hoping acks could just be completion blocks — this would be awesome:

socket.emitWithAck('create_game', ["name": playerName]) { data in
    // data is whatever the server acked back
}

but it seems that's not the case at all. I also noticed #11 is still open — does that mean acking isn't supported at all?

Thanks for the help!

Kyle

kylebshr commented 8 years ago

UPDATE — just figured out how it works! The API feels a little confusing though, and I would propose a change.

This seems to do what I need:

socket.emitWithAck(EmitEvent.create, ["name": playerName])(timeoutAfter: 5) { data in
    // do stuff
}

but this makes more sense in my opinion:

socket.emitWithAck(EmitEvent.create, ["name": playerName], timeout: 5) { data in
    // do stuff
}
nuclearace commented 8 years ago

Yeah, the reason I did that is because variadic parameters have to be last. And I didn't like having the timeoutAfter before the emit data.

nerezza07 commented 8 years ago

Hi @nuclearace I'm still figuring out how to use emitWithAck,, I'm very new to swift and socket (only started 2 days ago). I have this code in js

myApp.server.emit('auth/verify', req, function(error, data) { if ($.isFunction(callback)) { callback(myApp.app.error.process(error), data); } });

and in swift I do this socket.emitWithAck("auth/verify", req)(timeoutAfter:5, callback: {data in print(data)})

but seems like there is no data returned...

Am I going to the right direction? or its totally wrong?