iphone - 如何使用 GameKit 在 iPhone 蓝牙中区分主机和客户端

标签 iphone objective-c connection bluetooth multiplayer

我使用 GameKit 框架制作了一款多人游戏,其中 2 个 iPhone/iPod 可以通过蓝牙相互连接并玩游戏。

我正在考虑一种方法来选择哪个设备可以先玩。因此,合理的解决方案是选择连接的主机。 GKSession 中是否有服务器和客户端?他们都是同龄人吗?我应该采取哪条路线来实现我的需求?

最佳答案

基本上,您可以根据自己的喜好配置 session 。

摘自苹果文档:

Sessions discover other peers on the network based on a session mode which is set when the session is initialized. Your application can configure the session to be a server, which advertises a service type on the network; a client, which searches for advertising servers; or a peer, which advertises like a server and searches like a client simultaneously.

A copy of your application acting as a server initializes the session by calling initWithSessionID:displayName:sessionMode: with a session mode of either GKSessionModeServer or GKSessionModePeer. After the application configures the session, it advertises the service by setting the session’s isAvailable property to YES.

A copy of your application acting as a client initializes the session by calling initWithSessionID:displayName:sessionMode: with a session mode of either GKSessionModeClient or GKSessionModePeer. After configuring the session, your application searches the network for advertising servers by setting the session’s isAvailable property to YES. If the session is configured with the GKSessionModePeer session mode it also advertises itself as a server, as described above.

因此,如果您使用 GKSessionModePeer 来初始化 session ,您就拥有了对等点(既充当服务器又充当客户端)。如果您想区分服务器和客户端,请使用 GKSessionModeServer 对其进行初始化。

亲切的问候。

关于iphone - 如何使用 GameKit 在 iPhone 蓝牙中区分主机和客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1038192/

相关文章:

iphone - 如何在 Xcode 3.2.6 中使用较旧的 iOS 版本测试应用程序?

iphone - 邮件功能 iphone App 崩溃

c++ - 从 iOS 项目中使用的 C++ 类读取文件

iphone - FBLogin 按钮调整大小

Android ConnectException 错误 - EHOSTUNREACH(无路由到主机)错误

php - PDOConnection.php 第 24 行中的 FatalThrowableError Laravel 5.3

iphone - 显示错误iphone的描述

ios - objective c 如何为整个应用程序的所有 UI 按钮设置独占触摸

ios - 可以异步加载 UILabel/UITextView 的 NSAttributesString(从 HTML 生成)中的图像(iOS 7+)吗?

mysql - Wordpress - 建立数据库连接时出错