2

I have an app that does local networking using a GKSession running in GKSessionModeServer mode. This is limited so that only two devices will ever connect when using this app.

All of the connection and communication code works exactly as expected as long as the devices do not disconnect from each other. I want the devices to automatically reconnect if there is an interruption. I have been working on code so that a device will automatically reconnect after a disconnection event as happened.

I can run the app at the same time on a simulator & device, if I suspend the app on the simulator it reconnects exactly as I expect (whether running as a server or client), however neither code works on the device if I suspend the app on a real device.

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
    NSLog(@"Game: peer %@ changed state %d", peerID, state);

    if (state == GKPeerStateDisconnected) {
        if (_isServer) {
            NSLog(@"Switching session ON");
            _session.available = YES;
        } else {
            NSLog(@"Running a reconnection request");
            [_session connectToPeer:peerID withTimeout:_session.disconnectTimeout];
        }
        [self.delegate bridgeHandDelegateShowDisconnectedScreen:self];
    } else if (state == GKPeerStateConnected) {
        if (_isServer) {
            NSLog(@"Switching Session off");
            _session.available = NO;
        }
        [self.delegate bridgeHandDelegateHideDisconnectedScreen:self];
    }

    if (_isServer) {
        NSLog(@"session state: %d", _session.available);
    }
}

As you can see I have littered by code with NSLog statements, the output when I suspend the host is on the simulator is:

Simulator:

2013-03-22 18:28:55.359 Practice[56120:c07] Game: peer 587373189 changed state 3
2013-03-22 18:28:55.359 Practice[56120:c07] Switching session ON
2013-03-22 18:28:55.364 Practice[56120:c07] session state: 1
2013-03-22 18:28:56.563 Practice[56120:c07] Game: peer 587373189 changed state 4
2013-03-22 18:28:56.563 Practice[56120:c07] session state: 1
2013-03-22 18:28:56.564 Practice[56120:c07] Game: connection request from peer 587373189
2013-03-22 18:28:56.565 Practice[56120:c07] MatchmakingServer: Connection accepted from peer: 587373189
2013-03-22 18:28:56.579 Practice[56120:c07] Game: peer 587373189 changed state 2
2013-03-22 18:28:56.580 Practice[56120:c07] Switching Session off
2013-03-22 18:28:56.582 Practice[56120:c07] session state: 0

Device:

2013-03-22 22:24:04.922 BridgeBiddingPractice[3739:907] Game: peer 865951788 changed state 3
2013-03-22 22:24:04.924 BridgeBiddingPractice[3739:907] Switching session ON
2013-03-22 22:24:04.938 BridgeBiddingPractice[3739:907] session state: 0
2013-03-22 22:24:04.942 BridgeBiddingPractice[3739:907] Game: session failed Error Domain=com.apple.gamekit.GKSessionErrorDomain Code=-65540 "The operation couldn’t be completed. (com.apple.gamekit.GKSessionErrorDomain error -65540.)"

I can't find any mention of the error 65540. Any help would be gratefully received.

Marryat
  • 535
  • 3
  • 11

0 Answers0