【发布时间】:2010-12-27 21:19:16
【问题描述】:
我一直在尝试使用 5 个 iPod 通过蓝牙创建一个简单的客户端/服务器网络。一个 iPod 将向四个 iPod 发送诸如“停止”或“开始”之类的基本字符串,然后它们会在收到命令时执行各种操作。我目前正在使用没有 PeerPicker 的 GameKit,甚至启动连接都不可靠且复杂。连接后,我可以在两个设备之间正常发送数据,但不能更多。
我希望能够从没有网络的 iPod“服务器”(如遥控器)向 iPod“客户端”广播简单的短消息。我查看了许多示例,包括坦克和 wiTap 示例,但令我惊讶的是,没有什么比不需要网络更简单的了。
这是我开始连接的方式:
- (IBAction) btnConnect:(id)sender
{
if (sender == connectServer) {
self.currentSession = [[GKSession alloc] initWithSessionID:@"0000"
displayName:nil
sessionMode:GKSessionModeServer];
NSLog(@"Setup Server Connection");
} else { //connectClient
// Peers discover themselves ...
self.currentSession = [[GKSession alloc] initWithSessionID:@"0000"
displayName:nil
sessionMode:GKSessionModeClient];
NSLog(@"Setup Client Connection");
}
amAcceptingConnections = YES;
[self.currentSession peersWithConnectionState:GKPeerStateAvailable];
self.currentSession.delegate = self;
self.currentSession.disconnectTimeout = 30;
[self.currentSession setDataReceiveHandler:self withContext:nil];
// Advertise the session to peers
self.currentSession.available = YES;
}
我在委托中处理与 didChangeState 的连接
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
thePeerID = [session displayNameForPeer:peerID];
NSLog(@"didChangeState was called from peerID: %@.", thePeerID);
self.currentSession = session;
switch (state) {
case GKPeerStateAvailable:
thePeerID = [session displayNameForPeer:peerID];
NSLog(@"Peer %@ Available", thePeerID);
[session connectToPeer:peerID withTimeout:30];
NSLog(@"Issued Peer Connection");
//session.available = NO; //TODO: Look at this
break;
case GKPeerStateUnavailable:
NSLog(@"Peer %@ Unavailable", thePeerID);
break;
case GKPeerStateConnected:
NSLog(@"Peer %@ Connected", thePeerID);
break;
case GKPeerStateDisconnected:
NSLog(@"Peer %@ Disconnected", thePeerID);
[self.currentSession release];
currentSession = nil;
break;
}
}
问题是 didChangeState 方法在它良好且准备就绪时会触发,并且行为是不可预测的。也许这是蓝牙或 GameKit 的问题,但我想在没有其他 iPod“授权”连接的情况下进行连接。这可能吗?
认为我需要一个路由器和一个网络来完成这么简单的事情吗?
谢谢
【问题讨论】:
标签: iphone client-server messaging gamekit