Online Book Reader

Home Category

iPhone Game Development - Chris Craft [120]

By Root 1630 0
(NSString*)playerName;

- (void) apiSendMessage:(NSString*)message

asType:(GameConnectionMessageType)messageType;

@end

In GameConnection, the method connectWithPlayerName: is called when a player wants to initiate a new game with a friend. This method is actually implemented in the class InternetGameConnection. Walking through this call shows you how Internet connectivity is achieved. This method is simply a pass-through with a sentinel. The parameter playerName is checked with the sentinel and the pass-through call to apiConnectWithPlayerName is issued:

- (void) connectWithPlayerName:(NSString*)playerName {

[super connectWithPlayerName:playerName];

if ([playerName length] == 0)

[NSException raise:@”Parameter required”

format:@”Parameter playerName cannot be null or empty”];

[self apiConnectWithPlayerName:playerName];

}

The method apiConnectWithPlayerName issues a request to the Internet server, which begins the Web request process. This is the same process that is followed when you start a Google search in your browser.

Note

We have chosen to issue our request as an HTTP POST. With a POST, the parameters can be bundled as in the body of the request. This has two advantages: First, it's easy to extract the parameters in your server implementation; and second, you can switch to SSL, which encrypts the parameters that are embedded in the request body.

The method builds an NSMutableURLRequest and uses an NSURLConnection to issue the request with a call to connectionWithRequest. The call to connectionWithRequest is followed by a call to start, which starts the request process. The important thing to observe is that this is only the beginning of the process. InternetGameConnection implements the NSURLConnectionDelegate methods necessary to receive the server response:

- (void) apiConnectWithPlayerName: (NSString*)playerName {

NSString* content = [NSString stringWithFormat:@”playerName:%@”, playerName];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:

[NSURL URLWithString: [SERVICE_END_POINT stringByAppendingString:

@”connectWithPlayerName”]]];

[request setHTTPMethod: @”POST”];

[request setHTTPBody:[content dataUsingEncoding: NSASCIIStringEncoding]];

self.responseData = [NSString stringWithString:@””];

urlConnection = [NSURLConnection connectionWithRequest:request

delegate:self];

[urlConnection start];

}

After the request has been sent, NSURLConnection receives a response either in the form of data or an error (hopefully it's data). If you do receive data, it arrives as a call to the delegate method connection didReceiveData:. It is also possible—and likely—that the response will be too large to arrive in one piece. If this happens, the data is broken up and arrives in chunks. This results in multiple calls to connection:didReceiveData:. Each call appends the received chunk to the property responseData. Finally, connectionDidFinishLoading is fired, which informs you that all the pieces of the response are received and the complete reconstituted data can be retrieved from the property responseData:

#pragma mark NSURLConnection delegate methods

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

NSString *stringReply = [[NSString alloc] initWithData:data

encoding:NSUTF8StringEncoding];

self.responseData = [self.responseData stringByAppendingString:stringReply];

}

The data you receive from the server is entirely up to you and depends on how you wish to communicate with your Web server. If you are looking for a standard, REST (representational state transfer) is a good choice for iPhone development because making calls to REST is very straightforward. The class URLConnection is all you need on the client. There is a lot to implementing a Web server, a discussion that is outside the scope of this book. Check out www.appsamuckcom/gameservices for more information.

REST is not the only protocol available; Web services come in many different

Return Main Page Previous Page Next Page

®Online Book Reader