问题
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:
On each device, I hit my app's connect button. Each device then shows the "iPads, iPhones, or iPod touches" window.
Usually pretty quickly, the two devices each notice that the other exists.
I then tell one of the devices (say device A) to connect to the other.
Pretty quickly, device B's window says that device A is not available.
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.
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