views:

573

answers:

4

How would you guys go about creating online play capabilities for an iPhone game? Obviously, one could poll the server every so often, but is this realistic given the capabilities of the device? Assume you're polling the server every second or two and retrieving 100 bytes of data... Is it possible to retrieve the data in the background while gameplay continues or is it going to be held up by the server poll?

Thanks in advance, BCH

+1  A: 

You can do asynchromous requests. But you are asking a really, really hard question here. Just want to make sure you know that.

Squeegy
Yeah, I realize it's a difficult question, but I figured StackOverflow is the place to go for that. I'm more concerned with the feasibility than anything, not necessarily looking for source code.
Well yeah, its possible. The really hard part is trying to keep the game going between polls when you don't have any idea how long it will take for the server to respond.
Squeegy
Yeah, that's true, the application I have in mind won't be screwed if the data is a couple seconds delayed though, it's more of just a look-up to see what other players boards look like.
+5  A: 

You need to make a multithreaded application. Then you can have a background thread polling the server (or better, making asynchronous requests on it.

Have a look at the NSOperation and NSOperationQueue classes. You'll make each background task be represented by an instance of an NSOperation class. This class takes care of starting the operations, making sure that they are run in the appropriate order, and accounting for any priorities that you set.

The most common way to use NSOperation is to write a custom subclass and override the method, main. The main method gets called to perform the operation when the NSOperationQueue schedules it to run.

If you don't want the overhead of subclassing, take a look at the NSInvocationOperation class. This is a concrete subclass of NSOperation that makes it easy to attach an operation to an existing method. NSInvocationOperation objects can be added to an NSOperationQueue (just like NSOperations), so that you get multi-threading without having to subclass.

Jane Sales
+1  A: 

Hi,

Take a look at the WiTap example (in xcode do a full text search for witap). It uses NSStreams to get asynchronous behavior without you having to resort to managing your own threads or poll.

Bill Dudney
A: 

Use OpenFeint.. It allows you to develop Over the internet worldwide non real time turn based multiplayer games easily..

I'm not sure if GameCenter now supports similar functions.

Quakeboy