【发布时间】:2013-07-11 11:56:59
【问题描述】:
好的,这涉及到this part of a multiplayer tutorial的大量网络编码。
基本上,我正在尝试按照上面链接的教程使用 GameKit 实现多人游戏。我投入了所有必要的网络编码,或多或少地理解了它,但是我在方法调用的某个地方遇到了障碍。基本上,我的设置是一台设备充当主机,其余设备充当客户端。我有两个单独的UIViewcontrollers 分别用于建立连接的主机和客户端。
现在问题是,连接已建立,但只有主机识别连接,而不是客户端。问题出在这里:
- (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];
}
此方法在我上面链接的教程的下一部分(here)中,应该接收主机发送给所有客户端的数据包并实现此网络链中的下一个方法。但是,该方法永远不会被调用。没有触发调试断点,我在控制台中什么也得不到。
我知道我是否需要提供更多的源材料,但是已经实现了很多网络编码,所以我想把它限制在人们需要看到的范围内。另外,[_session setDataReceiveHandler:self withContext:nil] 和_session.delegate = self 是用另一个在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]]);
// }
}
【问题讨论】:
-
你是如何设置 GKSession 对象的? GKSession * gk_session = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
-
[_session setDataReceiveHandler:self withContext:nil];您在此处传递 self ,处理程序是您希望会话在从其他对等方接收数据时调用的对象。并且处理程序必须实现具有以下签名的方法: - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context;我想知道,您是否传递了正确的处理程序?
-
当它调用
[_session setDataReceiveHandler:self withContext:nil]方法时,self是GameMultiplayer,这就是我想要的数据处理程序。是的,正如我在问题中所展示的那样,您提到的方法在GameMultiplayer中被调用 -
那有什么问题?
-
问题是在程序调用
sendPacketToAllClients之后从未调用receiveData,即使教程声称它应该被调用
标签: ios objective-c gamekit