11

好的,这涉及多人教程的这一部分中的大量网络编码。

基本上,我正在尝试按照上面链接的教程使用 GameKit 实现多人游戏。我输入了所有必要的网络编码并且或多或少地理解了它,但是我在方法调用的某个地方遇到了障碍。基本上,我的设置是一台设备充当主机,其余设备充当客户端。我有两个单独UIViewcontroller的 s 分别用于建立连接的主机和客户端。

现在的问题是,连接已建立,但只有主机识别连接,而不是客户端。问题在这里:

- (void)sendPacketToAllClients:(Packet *)packet
{
    [_players enumerateKeysAndObjectsUsingBlock:^(id key, Player *obj, BOOL *stop)
     {
         obj.receivedResponse = [_session.peerID isEqualToString:obj.peerID];
     }];

    GKSendDataMode dataMode = GKSendDataReliable;
    NSData *data = [packet data];
    NSError *error;
    if (![_session sendDataToAllPeers:data withDataMode:dataMode error:&error])
    {
        NSLog(@"Error sending data to clients: %@", error);
    }
}

这是在 中实现的GameMultiplayer,实际游戏将在其中实现。这个方法应该做的是向每个客户端发送数据包,说主机收到了连接请求并且能够与它们连接。[_session sendDataToAllPeers:data withDataMode:dataMode error:&error]调用后(if语句中的方法),应该触发这个方法:

- (void)receiveData:(NSData *)data fromPeer:(NSString *)peerID inSession:(GKSession *)session context:(void *)context
{
#ifdef DEBUG
    NSLog(@"Game: receive data from peer: %@, data: %@, length: %d", peerID, data, [data length]);
#endif

    Packet *packet = [Packet packetWithData:data];
    if (packet == nil)
    {


        NSLog(@"Invalid packet: %@", data);
        return;
    }

    Player *player = [self playerWithPeerID:peerID];
    if (player != nil)
    {
        player.receivedResponse = YES;  // this is the new bit
    }

    if (self.isServer)
        [self serverReceivedPacket:packet fromPlayer:player];
    else
        [self clientReceivedPacket:packet];
}

这个方法在我上面链接的教程的下一部分(在这里),应该接收主机发送给所有客户端的数据包,并在这个网络链中实现下一个方法。但是,该方法永远不会被调用。没有触发调试断点,我在控制台中什么也没有。

我知道我是否需要提供更多的源材料,但是已经实现了很多网络编码,所以我想把它限制在人们需要看到的范围内。此外,[_session setDataReceiveHandler:self withContext:nil]and_session.delegate = self是用另一种方法编写的,称为 in GameMultiplayer,所以这不是问题。有谁知道我需要修复什么?

编辑:根据要求,这里是 GKSession 的初始化位置:

@property (nonatomic, strong, readonly) GKSession *session; //This is done in the header file

@synthesize session = _session; //This is done in the main file

- (void)startAcceptingConnectionsForSessionID:(NSString *)sessionID
{

    if (_serverState == ServerStateIdle)
    {
        _serverState = ServerStateAcceptingConnections;

        _connectedClients = [NSMutableArray arrayWithCapacity:self.maxClients];

        _session = [[GKSession alloc] initWithSessionID:sessionID displayName:nil sessionMode:GKSessionModeServer];
        _session.delegate = self;
        _session.available = YES;
    }
}

会话在 中初始化MatchmakingServer,在主机视图控制器中使用。然后会话被传递到应用程序的主视图控制器,然后初始化GameMultiplayer并将 GKSession 发送给它。这是主机视图控制器将其发送到主视图控制器的位置:

- (IBAction)startAction:(id)sender
{
    if (_matchmakingServer != nil && [_matchmakingServer connectedClientCount] > 0)
    {
        NSString *name = [self.nameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([name length] == 0)
                name = _matchmakingServer.session.displayName;

        [_matchmakingServer stopAcceptingConnections];

        [self.delegate hostViewController:self startGameWithSession:_matchmakingServer.session playerName:name clients:_matchmakingServer.connectedClients];
    }
}

然后主视图控制器在此处处理该方法调用:

- (void)hostViewController:(MatchmakerHost *)controller startGameWithSession:(GKSession *)session playerName:(NSString *)name clients:(NSArray *)clients
{

    [self dismissViewControllerAnimated:NO completion:^
     {

         [self startGameWithBlock:^(GameMultiplayer *aGame)
          {
              [aGame startServerGameWithSession:session playerName:name clients:clients];
          }];
     }];
}

最后,这是该方法调用的实现位置GameMultiplayer

- (void)startServerGameWithSession:(GKSession *)session playerName:(NSString *)name clients:(NSArray *)clients
{
    _clients = clients;

    const char* className = class_getName([[_clients objectAtIndex:0] class]);
    NSLog(@"yourObject is a: %s", className);

    self.isServer = YES;

    _session = session;
    _session.available = NO;
    _session.delegate = self;
    [_session setDataReceiveHandler:self withContext:nil];

    _state = GameStateWaitingForSignIn;

    [self.delegate gameWaitingForClientsReady:self];

    // Create the Player object for the server.
    Player *player = [[Player alloc] init];
    player.name = name;
    player.peerID = _session.peerID;
    player.position = PlayerPositionBottom;
    [_players setObject:player forKey:player.peerID];

    // Add a Player object for each client.
    int index = 0;
    for (NSString *peerID in clients)
    {
        Player *player = [[Player alloc] init];
        player.peerID = peerID;
        [_players setObject:player forKey:player.peerID];

        if (index == 0)
            player.position = ([clients count] == 1) ? PlayerPositionTop : PlayerPositionLeft;
        else if (index == 1)
            player.position = PlayerPositionTop;
        else
            player.position = PlayerPositionRight;

        index++;
    }

    NSLog(@"Players:");

    Packet *packet = [Packet packetWithType:PacketTypeSignInRequest];
    [self sendPacketToAllClients:packet];

//    for (int i = 0; i < [_players count]; i++) {
//        NSLog([NSString stringWithFormat:@"%@", [clients objectAtIndex:i]]);
//    }
}
4

2 回答 2

0

我也遇到了 GKSession 的麻烦。我今天有兴趣(在此站点上)了解 GKSession 已被弃用以支持使用Multipeer Connectivity Framework。幸运的是,Wenderlich等人。将使用新技术做一个教程。:)

该系统与 GKSession 有一些相似之处,因此不太难理解。

苹果的文档链接

于 2013-09-25T08:17:53.367 回答
0

我认为您正在快速调用发送。当服务器意识到连接时,它会向客户端发送确认以真正建立连接 - 所以客户端知道它成功了。

如果您在此之前发送数据包 - 它将丢失。

只需这样做:

[self performSelector:@selector(sendPacketToAllClients) withObject:nil afterDelay:1.0];

代替:

[self sendPacketToAllClients];

我遇到了同样的问题,即连接是在不同的时刻建立的,客户端的延迟很小。最好的办法是从客户端发送第一个数据包,他已准备好从服务器接收数据包 - 然后从那里正常进行。

也尝试调试:

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

在两个设备(服务器和客户端)上。

于 2013-09-10T10:33:20.927 回答