GameKit服务器/客户端

时间:2010-09-07 02:48:32

标签: objective-c cocoa-touch bluetooth gamekit

我一直在尝试实现用于蓝牙连接的GameKit框架,并希望使用服务器/客户端关系来减少延迟并能够区分连接的设备。我找到了this thread,它与我想要做的类似,但代码对我不起作用。这就是我所拥有的:

连接方法:

-(IBAction) btnConnect:(id) sender {
if(sender == server){
    [self.currentSession initWithSessionID:@"BT" displayName:nil sessionMode:GKSessionModeServer];
    currentSession.available == YES;
    NSLog(@"Setup Server");
}else{
    [self.currentSession initWithSessionID:@"BT" displayName:nil sessionMode:GKSessionModeClient];
    currentSession.available == YES;
    NSLog(@"Setup Client");
}
currentSession.delegate = self;
currentSession.disconnectTimeout = 0;
[currentSession setDataReceiveHandler:self withContext:nil];
[client setHidden:YES];
[server setHidden:YES];
[disconnect setHidden:NO];       
}

didChangeState:

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
NSLog(@"didChangeState was called with status: %@.", state);
switch (state)
{
    case GKPeerStateConnected:
        NSLog(@"connected");
        break;
    case GKPeerStateDisconnected:
        NSLog(@"disconnected");
        [self.currentSession release];
        currentSession = nil;
        [connect setHidden:NO];
        [disconnect setHidden:YES];
        break;
    case GKPeerStateAvailable:
    NSLog(@"Server is Available, Presenting UIALert...");
    NSLog(@"%@", peerID);
    peerName = [session displayNameForPeer:peerID];
    NSLog(@"%@", peerName);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Available!" message:[NSString stringWithFormat:@"The Server %@ is Available, Would you like to Connect?", peerName] delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
    [alert show];
    [alert release];
    if(selection == @"accept"){
        [session connectToPeer:peerID withTimeout:15];
        session.available = NO;
    }else{
    }
            break;
      }
 }

didReceiveConnectionRequest:

- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID{
NSLog(@"Recieved Connection Request");
NSLog(@"%@", peerID);
peerName = [session displayNameForPeer:peerID];
NSLog(@"%@", peerName);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Request" message:[NSString stringWithFormat:@"The Client %@ is trying to connect.", peerName] delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
[alert show];
[alert release];
if(selection == @"accept"){
    [session acceptConnectionFromPeer:peerID error:nil];
}else{
    [session denyConnectionFromPeer:peerID];
}
}

我想我的所有设置都正确,但是没有调用didChangeState来通知用户另一个设备可用。我错过了什么或者我应该尝试使用不同的方法。谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

currentSession.disconnectTimeout = 0;

断开连接超时是对等体在断开无响应的对等体之前应该等待的时间(以秒为单位)。你不希望它为0.默认值是20秒,你应该把它留在那里或说10秒钟。我实际上没有在我的GameKit代码中设置它并且它运行良好。

此外,将您的整个实现类发布到某个地方可能会有所帮助。我们需要确保您正在实施GKSessionDelegate,例如:

@interface SomeObject : NSObject <GKSessionDelegate>

此外,您正在设置上面的Peer-2-Peer。你说你试图做客户端/服务器。如果是这样,您应该使用GKSessionModeClient模式和服务器GKSessionModePeer启动客户端会话。

最后......你在实际设备或设备和模拟器上进行测试吗?不要忘记,模拟器和第一代iPhone和触摸不支持蓝牙。所以你必须让每个人都参与到同一个无线网络中,才能发生任何事情。

启动调试会话时,您在控制台中看到了什么?

相关问题