iPhone p2p - Is there a way to connect to more tha

2019-02-01 13:27发布

Is it possible to connect to more than 1 devices using the new GameKit framework? Till now all the examples I've seen show how we can connect to 1 device. Even the default connection interface lets the user select just one connect.

Thanks.

3条回答
地球回转人心会变
2楼-- · 2019-02-01 13:55

There's a fairly good overview here at the following link of how to configure and use GameKit for your App.

Unfortunately, you will not be able to use the GKPeerPickerController standard UI picker to configure the GKSession instance for you if you want to support more than 2 devices. Instead, you must develop your own UI elements to setup and configure the connections between the master/server and the multiple clients.

Here's a simple way to enable GameKit for more than 2 devices:

When you initialize a GKSession instance with initWithSessionID:displayName:sessionMode, use:

  • sessionMode:GKSessionModeServer for the server/master
  • sessionMode:GKSessionModeClient for the other (multiple) clients

sessionMode:GKSessionModePeer is the 3rd available option and is the option used in most sample code provided by Apple such as the GKTank demo.

Note: You can probably use GKSessionModePeer and still have a network of more than 2 devices since peer mode puts the device in server and client mode at the same time until a connection is made. Explicitly configuring one device as the server may simplify the setup which is useful for testing the GameKit API.

For the server:

GKSession *session = [[GKSession alloc] initWithSessionID:kTestSessionID displayName:nil sessionMode:GKSessionModeServer]; 

For the client(s):

GKSession *session = [[GKSession alloc] initWithSessionID:kTestSessionID displayName:nil sessionMode:GKSessionModeClient]; 
查看更多
\"骚年 ilove
3楼-- · 2019-02-01 13:59

To the best of my knowledge, when using the GameKit framework you can only couple a pair of devices, one acting as a server and the other as a client (if both are initialized as peers, you will have to decide which one will act as a server, either arbitrarily or by selecting one randomly).

In practice, there may be many devices all running your application, but connections will always be established through paired devices.

Of course, you can write your own bonjour-based networking code over wifi and overcome this limit though it will require much more coding.

查看更多
一纸荒年 Trace。
4楼-- · 2019-02-01 14:08

In the past I've read you can connect up to sixteen devices using Gamekit. I believe that you can only do two when in Peer mode, to do more than two requires one device be set as master and the others in client mode.

I'd look for examples that demonstrate use of master/client, the client side should apply to all three.

You can call +maxPlayersAllowedForMatchOfType: on GKMatchRequest (iOS6+) to get the actual number, which could vary per device so you should probably use that as an upper bounds.

查看更多
登录 后发表回答