GameKit:向其他玩家发送可靠的 NSData?

发布于 2024-10-29 01:04:20 字数 176 浏览 1 评论 0原文

好的,我成功地在两名玩家之间创建了一场比赛。现在我将做一个关于向玩家发送数据的小测试。

我不太明白关于 NSData 的解释。本质上,它是什么?我如何将一个不知道的数组发送给另一个玩家?

苹果提到数据包可能会丢失。但有一种“可靠”模式可以防止这种情况发生。但我找不到这样的。

有什么想法吗?

Okay, I managed to create a match between two players. Now I will do a little test about sending data to a player.

I didn't quite get the explanation about NSData. Essentially, what is it? How do I send a, dunno, array to another player?

Apple mentioned data packets could be lost. But there was a "reliable" mode to prevent that. But I couldn't find about such.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那片花海 2024-11-05 01:04:20

是的,您必须制定自己的协议。 “制定协议”是你自己做的事情。就像......本质上是在服务器端

// the message arrives and you then do this...
[data getBytes:&getMe length:sizeof(CommsProt)];

,而在客户端发送消息,你这样做......

NSData * data = [NSData dataWithBytes:&sendMe length:sizeof(CommsProt)];
// ...now send that data using GameKit or whatever system you end up with

并且你定义你的协议 - 至少是数据块 - 像这样:

typedef struct _CommsProt
    {
    BOOL        happyThing;
    someThings  wotJustHappened;
    float       happyValue;
    float       anotherHappyValue;
    // etc
    }
    CommsProt;

如果你是 GK 的新手,请注意这个关键提示...

客户端/服务器 GKSessions

这也可以帮助...

在 Mac/iPhone 上进行联网的最有效方法?

一些有用的注释...

(i)“客户端”和“服务器”意味着没有。您将能够以任一方向发送手提包信息(例如上面的“CommsProt”)。如果您想考虑并引用一个服务器端(特别是如果您有集线器类型的安排),那很好。但它只存在于你的脑海中。 (顺便说一句,通常您可能会在每个方向使用不同的数据结构,这完全没问题。)

(ii) 关于套接字。如果您热衷于网络,则必须处理套接字并编写自己的套接字代码。然而,您很可能可以选择一个网络层,您甚至不必说“套接字”这个词!例如,GameKit + Bonjour 完全为您处理套接字,并且该组合可以解决另一个极其困难的问题,即查找您的客户端/服务器之一。如果您是新手,我建议您暂时完全保留套接字,并使用 GameKit(或 Windows 上的等效系统)等系统作为网络层。

(iii) 事实上,首先只需使用像 GameKit 这样极其简单的东西,同时弄清楚你的协议和所有其他令人头疼的问题。稍后,如果有必要,您可以重写网络层,或者切换到您听说过的其他软件包。幸运的是,上述代码示例之前的所有内容都将保持不变,只有网络层会发生变化。

(iv) 关于 WiFi。 GameKit 和大多数便利包与物理层完全无关:它们会为您处理好它。无论手机如何连接,GK 都能正常工作——蓝牙、以太网等等!事实上,您“不会知道”正在使用哪个物理层。 (有时你无法控制这一点,这很烦人:随它去吧,用户不在乎。)

Yes, you have to make your OWN protocol. "making a protocol" is something you do yourself. Something like...Essentially on the server end

// the message arrives and you then do this...
[data getBytes:&getMe length:sizeof(CommsProt)];

whereas on the client end to send messages, you do this...

NSData * data = [NSData dataWithBytes:&sendMe length:sizeof(CommsProt)];
// ...now send that data using GameKit or whatever system you end up with

and you define your protocol - at least the chunks of data - like this:

typedef struct _CommsProt
    {
    BOOL        happyThing;
    someThings  wotJustHappened;
    float       happyValue;
    float       anotherHappyValue;
    // etc
    }
    CommsProt;

If you are new to GK, be aware of this critical tip ...

Client/Server GKSessions

This could also help...

Most effective way to do networking on Mac/iPhone?

Some helpful notes...

(i) "client" and "server" mean nothing. You will be able to send the handbags of information (such as "CommsProt" above) in either direction. If you want to think of, and refer to one, end as a server (particularly if you have a hub-type arrangement), that's fine. But it's only in your head. (By the way, commonly you might use a different data structure in each direction, that's perfectly fine.)

(ii) Regarding sockets. If you get heavily in to networking, you will have to deal with sockets and write your own sockets code. However it is very likely you can choose a networking layer where you never even have to say the word "sockets"! GameKit + Bonjour, for example, completely take care of handling sockets for you, AND that combination takes care of the other incredibly difficult issue which is FINDING one of your client/servers. If you are new I recommend you completely set aside sockets for now, and use a system such as GameKit (or whatever is equivalent on Windows) for your networking layer.

(iii) Indeed AT FIRST just use something incredibly simple like GameKit, while you figure out your protocol and all the other headaches. Later, if necessary you can rewrite the networking layer, or, switch to some other package that you hear about. Happily everything up to the code examples above will be unchanged, only the networking layer will change.

(iv) Regarding WiFi. GameKit and most convenience packages, are completely agnostic to the physical layer: they take care of it for you. GK will work fine however the phones are connected -- bluetooth, ethernet, whatever! Indeed you "won't know" what physical layer is being used. (It's sometimes annoying you can't control this: let it go, users couldn't care less.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文