Search code examples
iphoneobjective-ciosgame-centermultiplayer

How to tell when we are disconnected from GameCenter GKMatch session?


I'm wondering how do I get the disconnect message for local player when the game session is in progress and we're unable to communicate our data to other players. As there is nothing in documentation that says "this method will inform you whenever your connection fails", I'm at a bit of a loss.

I was trying to use this chunk of code in hopes that it would work, but it's futile. The "We're disconnected." message is never triggered.

- (void)match:(GKMatch *)theMatch player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state {
  if (self.match != theMatch) return;

  switch (state) {

    case GKPlayerStateDisconnected:
      //disconnected
      NSLog(@"player status changed: disconnected");
      matchStarted = NO;
      GKLocalPlayer *player = [GKLocalPlayer localPlayer];
      if ([playerID isEqualToString:player.playerID]) {

        // We have been disconnected
        NSLog(@"We're disconnected.");
      }
      if ([delegate respondsToSelector:@selector(matchEnded)]) {
        [delegate matchEnded];
      }
      break;
  }
}

The only other line that I found might tell us that we're unable to communicate is when we actually send data like this:

- (void)sendRandomMatchData:(NSData *)data {  
  GKMatch *match = [GCHelper sharedInstance].match;
  BOOL success = [match sendDataToAllPlayers:data
                                withDataMode:GKMatchSendDataReliable
                                       error:nil];
  if (!success) {
    [self matchEnded];
  }
}

But I assume that "success" will also be false if the opponent has disconnected and we're unable to send messages to them.

I have a pretty strict game logics, if someone has been disconnected I need to inform them that they are unable to continue playing the match and that they have lost.

Any help is highly appreciated.


Solution

  • What about examining the error after the following code line:

      BOOL success = [match sendDataToAllPlayers:data
                                    withDataMode:GKMatchSendDataReliable
                                           error:nil]; //replace nil with NSError variable
    

    Maybe error will give you extra info u need.
    Another idea is to create NSTimer and set some certain time for making moves/turns. If some player didn't make it for a certain time then assume this player is disconnected. Also you could check your Internet connection state to determine you have a connection cuz maybe you just lost it and that's the reason you can't send/receive any data.
    Also you could check every player periodically by sending them some short amount of data using GC just to make them answer you. This way you could ensure all players are "alive" or detect some "zombie".
    Remember if player moves the game to background using Home button you won't detect it anyhow cuz code in your game wont execute. And this situation doesn't mean that player is "zombie". Player could be busy by a call or something else like another app. Player could temporary loose Internet connection. This means that he/she could return to game soon...