GameKit 服务器/客户端
我一直在尝试实现用于蓝牙连接的 GameKit 框架,并希望使用服务器/客户端关系来减少延迟并能够区分连接的设备。我发现这个帖子< /a> 它与我想要做的类似,但代码对我不起作用。这是我所拥有的:
Connect Method:
-(IBAction) btnConnect:(id) sender {
if(sender == server){
[self.currentSession initWithSessionID:@"BT" displayName:nil sessionMode:GKSessionModeServer];
currentSession.available == YES;
NSLog(@"Setup Server");
}else{
[self.currentSession initWithSessionID:@"BT" displayName:nil sessionMode:GKSessionModeClient];
currentSession.available == YES;
NSLog(@"Setup Client");
}
currentSession.delegate = self;
currentSession.disconnectTimeout = 0;
[currentSession setDataReceiveHandler:self withContext:nil];
[client setHidden:YES];
[server setHidden:YES];
[disconnect setHidden:NO];
}
didChangeState:
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
NSLog(@"didChangeState was called with status: %@.", state);
switch (state)
{
case GKPeerStateConnected:
NSLog(@"connected");
break;
case GKPeerStateDisconnected:
NSLog(@"disconnected");
[self.currentSession release];
currentSession = nil;
[connect setHidden:NO];
[disconnect setHidden:YES];
break;
case GKPeerStateAvailable:
NSLog(@"Server is Available, Presenting UIALert...");
NSLog(@"%@", peerID);
peerName = [session displayNameForPeer:peerID];
NSLog(@"%@", peerName);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Available!" message:[NSString stringWithFormat:@"The Server %@ is Available, Would you like to Connect?", peerName] delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
[alert show];
[alert release];
if(selection == @"accept"){
[session connectToPeer:peerID withTimeout:15];
session.available = NO;
}else{
}
break;
}
}
didReceiveConnectionRequest:
- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID{
NSLog(@"Recieved Connection Request");
NSLog(@"%@", peerID);
peerName = [session displayNameForPeer:peerID];
NSLog(@"%@", peerName);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Request" message:[NSString stringWithFormat:@"The Client %@ is trying to connect.", peerName] delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
[alert show];
[alert release];
if(selection == @"accept"){
[session acceptConnectionFromPeer:peerID error:nil];
}else{
[session denyConnectionFromPeer:peerID];
}
}
我想我的所有设置都是正确的,但是 didChangeState 没有被调用来通知用户另一个设备可用。我是否遗漏了什么或者我应该尝试使用不同的方法。感谢您的帮助
I have been trying to implement the GameKit Framework for bluetooth connection and want to use a Server/Client relationship to reduce lag and be able to distinguish between to connected devices. I found this thread and it is similar to what I am trying to do, but the code doesn't work for me. Here is what I have:
Connect Method:
-(IBAction) btnConnect:(id) sender {
if(sender == server){
[self.currentSession initWithSessionID:@"BT" displayName:nil sessionMode:GKSessionModeServer];
currentSession.available == YES;
NSLog(@"Setup Server");
}else{
[self.currentSession initWithSessionID:@"BT" displayName:nil sessionMode:GKSessionModeClient];
currentSession.available == YES;
NSLog(@"Setup Client");
}
currentSession.delegate = self;
currentSession.disconnectTimeout = 0;
[currentSession setDataReceiveHandler:self withContext:nil];
[client setHidden:YES];
[server setHidden:YES];
[disconnect setHidden:NO];
}
didChangeState:
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
NSLog(@"didChangeState was called with status: %@.", state);
switch (state)
{
case GKPeerStateConnected:
NSLog(@"connected");
break;
case GKPeerStateDisconnected:
NSLog(@"disconnected");
[self.currentSession release];
currentSession = nil;
[connect setHidden:NO];
[disconnect setHidden:YES];
break;
case GKPeerStateAvailable:
NSLog(@"Server is Available, Presenting UIALert...");
NSLog(@"%@", peerID);
peerName = [session displayNameForPeer:peerID];
NSLog(@"%@", peerName);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Available!" message:[NSString stringWithFormat:@"The Server %@ is Available, Would you like to Connect?", peerName] delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
[alert show];
[alert release];
if(selection == @"accept"){
[session connectToPeer:peerID withTimeout:15];
session.available = NO;
}else{
}
break;
}
}
didReceiveConnectionRequest:
- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID{
NSLog(@"Recieved Connection Request");
NSLog(@"%@", peerID);
peerName = [session displayNameForPeer:peerID];
NSLog(@"%@", peerName);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Request" message:[NSString stringWithFormat:@"The Client %@ is trying to connect.", peerName] delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
[alert show];
[alert release];
if(selection == @"accept"){
[session acceptConnectionFromPeer:peerID error:nil];
}else{
[session denyConnectionFromPeer:peerID];
}
}
I think I have this all setup right, but the didChangeState isn't getting called to inform the user that another device is available. Am I missing something or should I try to use a different method. Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
断开连接超时是对等点在断开无响应的对等点之前应等待的时间(以秒为单位)。您不希望该值为 0。默认值为 20 秒,您应该将其留在那里或设置为 10 秒。实际上,我没有在 GameKit 代码中设置它,但它运行良好。
另外,将整个实现类发布到某个地方可能会有所帮助。我们需要确保您正在实现 GKSessionDelegate,例如:
此外,您正在上面设置一个 Peer-2-Peer。你说你正在尝试做客户端/服务器。如果是这样,您应该使用
GKSessionModeClient
模式启动客户端会话,并使用GKSessionModePeer
模式启动服务器。最后......您是在实际设备上还是使用设备和模拟器进行测试?不要忘记模拟器和第一代 iPhone 和触摸屏不支持蓝牙。因此,您必须让所有相关人员都连接到同一无线网络才能发生任何事情。
当您启动调试会话时,您在控制台中看到了什么?
The disconnect timeout is a time in seconds that peers should wait before disconnecting unresponsive peers. You don't want this to be 0. The default is 20 seconds, you should leave it there or say like 10 seconds. I actually don't set this in my GameKit code and it works well.
Also, it might help to post your entire implementation class somewhere. We'll need to make sure you are implementing GKSessionDelegate, e.g.:
Also, you're setting up a Peer-2-Peer above. You said you were trying to do client/server. If so you should start the client session with a mode of
GKSessionModeClient
and server asGKSessionModePeer
.Lastly...are you testing this on actual devices or with a device and simulator? Don't forget that simulator and first gen iPhones and touches do not support bluetooth. So you'll have to have everyone involved connected to the same wireless network for anything to happen.
What are you seeing in the console when you start up your debug session?