【发布时间】:2015-02-25 16:38:12
【问题描述】:
我将其用作服务器-客户端对等方式,因此只有一台设备上的代码决定其他设备的结果。
所以,我并没有很深入。但我正在为连接的玩家建立一个大厅。
客户端使用浏览器,当建立连接时,完成按钮将他们推送到LobbyViewController
现在,匹配设置和详细信息在HostViewController 中设置,然后转移到lobbyViewController 所以它们是为host 设置的
然后,如果客户端说_matchName 等于nil,那么它会向host 发送消息,host 可以读取此消息。然后作为回报发回_matchName。
但是,我需要在此应用程序的其余部分发送大量消息。我应该说发送和接收。而且我不想要很多
if (_matchName == nil) {
NSData *dataToSend = [@"getMatchName" dataUsingEncoding:NSUTF8StringEncoding];
NSArray *allPeers = _appDelegate.mcManager.session.connectedPeers;
NSError *error;
[_appDelegate.mcManager.session sendData:dataToSend
toPeers:allPeers
withMode:MCSessionSendDataReliable
error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
}
}
-(void)didReceiveDataWithNotification:(NSNotification *)notification {
MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
NSString *peerDisplayName = peerID.displayName;
NSData *receivedData = [[notification userInfo] objectForKey:@"data"];
NSString *receivedText = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"From: %@ message: %@", peerDisplayName, receivedText);
}
对于第一个 if,我不想为了一条消息而费尽心思,也不想以相同的形式创建响应。然后阅读响应消息并更新_matchName 属性.. 似乎有很多代码,用于简单更新 matchName..
如何,或者有没有办法创建一种方法来使这变得更简单?
【问题讨论】:
标签: ios networking send multipeer-connectivity