2012-09-14 76 views
1

如果我有1個服務器和幾個客戶端(所有的同行都知道會話名稱) - 這是沒有問題的。 但2服務器我有問題。 客戶端如何知道會話ID,連接到服務器的會話創建是什麼,他們如何選擇服務器?我不想使用同伴選取器。 Ty爲任何答案。遊戲工具包幾個服務器和幾個客戶端

+0

self.session = [[GKSession的alloc] initWithSessionID:SESSION_ID(如何知道)顯示名:無sessionMode:GKSessionModeClient]; –

+0

如果我有2個不同的服務器和1個sessionID。發生什麼事? –

回答

0
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.serverArray = [NSMutableArray array]; 

    self.session = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModeClient]; 

    self.session.delegate = self; 

    [self.session setDataReceiveHandler:self withContext:NULL]; 

    self.session.available = YES; 
} 

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state { 

    NSString *serverName = [session displayNameForPeer:peerID]; 

    [self.serverArray addObject:serverName]; 

    [self.availableServers reloadData]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [self.serverArray objectAtIndex:indexPath.row]; 

    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    ClientGameViewController *clientGameViewController = [[ClientGameViewController alloc] init]; 

    clientGameViewController.serverName = [self.serverArray objectAtIndex:indexPath.row]; 

    clientGameViewController.serverSessionName = [self.serverArray objectAtIndex:indexPath.row]; 

    [self.view addSubview:clientGameViewController.view]; 
} 
相關問題