Connection trouble with GKPeerPickerController

醉酒当歌 提交于 2019-12-14 01:39:24

问题


I am working on an iOS game with GKSession and GKPeerPickerController. I am finding that the process of connecting two iOS devices is slow and unreliable, even if they are right next to each other. Let's call my devices "device A" and "device B."

Here is what typically happens:

  1. On each device, I hit my app's connect button. Each device then shows the "iPads, iPhones, or iPod touches" window.

  2. Usually pretty quickly, the two devices each notice that the other exists.

  3. I then tell one of the devices (say device A) to connect to the other.

  4. Pretty quickly, device B's window says that device A is not available.

  5. This is where it gets annoying. After a period of time ranging from about 1 second to never, a window pops up on device B saying that device A wants to connect, and asking device B whether or not it wants to accept the connection.

  6. If the above window does pop up, everything proceeds normally. But this sometimes takes a long time, and sometimes does not happen at all.

I have three devices, and I have similar problems no matter which two I am using.

Is there anything I can do about this?

My connection method is pretty straightforward:

-(void) findPeerPickerOpponents {
    GKPeerPickerController *picker;
    picker = [[GKPeerPickerController alloc] init]; 
    picker.delegate = self;
    [picker show]; 
    [self.connectionDelegate disableIdleTimer];
}

回答1:


ok, what you should do:
- lets say you have device A,B and C
- device A is scanning, device B just started scanning
- device A sees device B as new peer - it sends invite to B
- device B gets invite - it automaticaly approves it, both devices are connected, no notification is presented to user, except that devices appear in the lobby
- device C shows up
- devices A and B try to connect to it, they get autoconnected and show up at lobby
- player touches device B on device A
- device A sends WANNAPLAY packet to B
- B presents pop up asking if player wants play with A - player answers YES
- device B sends ACCEPTED to A and disconnects everybody except A (by sending DISCONNECT)
- device A gets ACCEPTED and disconnects everybody except B (by sending DISCONNECT)
- device C gets 2x DISCONNECT and removes A and B from it's lobby

It works MUCH BETTER than the standard approach, even if its like 10x more work - I did it both ways and it's a very good solution.




回答2:


I gave up on peerPickerController completely. Check out Apple's GKRocket example code. - Uses GKSession and tableviews of peers. It is a bit buggy on my devices right now, for reasons I am not clear on, but I used the basic classes: GameLobby and SessionManager to get devices hooking up pretty well. Also has the advantage of not restricting you to one peer.



来源:https://stackoverflow.com/questions/6052661/connection-trouble-with-gkpeerpickercontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!