2011-03-31 55 views
0

我現在在基本級別上使用GameKit。我可以連接兩個設備並在它們之間發送消息。使用GameKit連接第三個設備

我有3臺設備,我們會打電話給他們裝置A,B和C

我能A連接到B,A至C和B到C,作爲單獨的設置。

如果我將A連接到B,然後嘗試將B連接到C,則設備C將顯示設備B可用,但設備B繼續旋轉並說「尋找可用iPod,iPhone ...」

peerPickerController:sessionForConnectionType:中,當我試圖將B連接到C時,我試圖讓設備B重複使用它在其連接中使用的相同GKSession ...因爲如果我在設備B上創建新會話,能夠連接到設備C,但下降到設備A.

這裏的連接是sessionForConnectionType

-(GKSession*)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type { 

    // session is a synthesized GKSession 
     if (session == nil) { 
      session = [[GKSession alloc] initWithSessionID:nil displayName:@"" sessionMode:GKSessionModePeer]; 
      session.delegate = self;   
     } 


     return session; 
    } 

回答

4

我結束了一個服務器/客戶端設置,這是更容易管理。這樣,只有一個服務器PeerID,而不是每個連接的服務器PeerID。我無法找到很多好的例子,所以我在這裏包含了基本的GameKit服務器/客戶端代碼。

// if the device in an ipad, treat it as a host/server 
if ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"]) { 
     isHost = YES; 
    } else { 
     isHost = NO; 
    } 

// monitor if this device is connected to another device 
    isConnected = NO; 
} 




#pragma mark GameKit Methods 

// Called when a change in the connection state is detected 
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state { 

    NSLog(@"Session:Peer:%@ Did Change State", peerID); 
    Globals *globals = [Globals shareData]; 

    switch (state) { 
     case GKPeerStateConnected: 
      NSLog(@"GKPeerStateConnected"); 

      [globals.localSession setDataReceiveHandler:self withContext:nil]; 

// if this device is not the host and is not connected yet... 
      if (!isHost && !isConnected) { 

// update variables, save the Server PeerId and the local Session so we can use them later 
       isConnected = YES; 
       serverSession = peerID]; 
       localSession = session; 
      } 
      break; 

     case GKPeerStateDisconnected: 
      NSLog(@"GKPeerStateDisconnected"); 
      break; 

     case GKPeerStateAvailable: 
      NSLog(@"GKPeerStateAvailable"); 
      if (!isHost) { 
       NSLog(@"Attempting to Connect..."); 
// the server is available, try to connect to it 
       [session connectToPeer:peerID withTimeout:20]; 
      } 
      break; 

     case GKPeerStateConnecting: 
      NSLog(@"GKPeerStateConnecting"); 
      break; 

     case GKPeerStateUnavailable: 
      NSLog(@"GKPeerStateUnavailable"); 
      break; 
    } 

} 

// Called if this device receives a request for a connection 
// This should only happen on the Server device 
-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID { 
    NSLog(@"Received Connection Request From %@", peerID); 

// Accept the connection request from the peer 
    [session acceptConnectionFromPeer:peerID error:nil]; 
} 

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context { 
    NSLog(@"Received Data"); 
} 

-(void)session:(GKSession *)session didFailWithError:(NSError *)error { 
    NSLog(@"Session Failed: %@", error); 
} 


// Connected to a UIButton 
-(IBAction)sendData { 
    NSLog(@"Sending Data ..."); 
} 

// Connected to a UIButton 
-(IBAction)beginConnection { 

    Globals *globals = [Globals shareData]; 

// Set this up as a server 
    if (isHost) { 
     GKSession *session = [[GKSession alloc] initWithSessionID:@"" displayName:@"Server" sessionMode:GKSessionModeServer]; 
     session.delegate = self; 
     session.available = YES;  
     NSLog(@"Setting Server Session Peer:%@", session.peerID); 
     globals.localSession = session; 
    } 

// or set it up as a client 
else { 
     GKSession *session = [[GKSession alloc] initWithSessionID:@"" displayName:nil sessionMode:GKSessionModeClient]; 
     session.delegate = self; 
     session.available = YES; 
     NSLog(@"Setting CLIENT Session Peer:%@", session.peerID); 
     globals.localSession = session; 
    } 

} 


... Dealloc, etc... 

@end 
+0

嘿克里斯,我期待實現iOS設備的客戶端服務器模型。我沒有找到很多關於這方面的信息,你能建議我使用一些指針或資源嗎? – slonkar 2011-11-10 02:17:41

+0

@SumitLonkar這是我寫的關於這個帖子的文章(http://stackoverflow.com/a/12738287/1079207) – 2012-10-14 00:40:06