简体   繁体   English

安装在同一局域网内不同设备上的 iOS 应用程序之间的通信

[英]Communication between iOS app installed on different devices on the same LAN

I have just delivered a prototype for a big client, everything was fine but I'm now curious to know if the solution/architecture I've chosen was the right one or there's place for improvement in case the project will keep on.我刚刚为一个大客户交付了一个原型,一切都很好,但我现在很想知道我选择的解决方案/架构是否正确,或者是否有改进的地方,以防项目继续进行。

The task was to build two iOS apps: one running on 5 different iPhones, and another running on 2 iPads.任务是构建两个 iOS 应用程序:一个在 5 部不同的 iPhone 上运行,另一个在 2 个 iPad 上运行。 Basically the iPhone applications had to communicate information to the iPads, and occasionally they also had to send information between each other (iPhone to iPhone).基本上,iPhone 应用程序必须与 iPad 通信,有时它们还必须在彼此之间(iPhone 到 iPhone)发送信息。 All the infos where small JSON objects/chunks whose size was small, really small.大小很小的小型 JSON 对象/块的所有信息,非常小。

The app was not intended to reach the app store, is a working prototype to test out some ideas in a user testing environment.该应用程序不打算进入应用程序商店,它是一个工作原型,用于在用户测试环境中测试一些想法。

I discarded bluetooth because we are talking about a peer-to-peer communication, not a one-to-one.我放弃了蓝牙,因为我们谈论的是点对点通信,而不是一对一。

What I did was to use web sockets thanks to SocketIO , through a small Node.js server that was running on my mac.由于SocketIO ,我所做的是通过在我的 mac 上运行的小型 Node.js 服务器来使用网络套接字。 The server was really simple, just receiving the messages from the clients and broadcasting information to the other ones.服务器非常简单,只是接收来自客户端的消息并向其他客户端广播信息。

What do you think?你怎么认为? Is the solution I've chosen ok, or there are better ones?我选择的解决方案是否合适,还是有更好的解决方案?

For example, this morning I've just found out these thread here on SO , and I've discovered I could have used GameKit.例如,今天早上我刚刚在 SO 上发现了这些线程,并且我发现我可以使用 GameKit。 What do you think?你怎么认为?

Socket.IO is nice because it is fairly simple to implement but it has the downside of requiring a central server. Socket.IO 很好,因为它实现起来相当简单,但它的缺点是需要中央服务器。 If you wanted to avoid that, you could use the Multipeer Connectivity framework that was introduced in iOS 7.如果您想避免这种情况,您可以使用 iOS 7 中引入的 Multipeer Connectivity 框架。

It will let you create one-to-one communication channels between devices on either the same WiFi network or Bluetooth.它将让您在同一 WiFi 网络或蓝牙上的设备之间创建一对一的通信通道。 Once the channel is created, you can send whole NSData objects (or create streams but it doesn't seem relevant to your use case).创建通道后,您可以发送整个NSData对象(或创建流,但它似乎与您的用例无关)。

A good read : http://nshipster.com/multipeer-connectivity/一个很好的阅读: http : //nshipster.com/multipeer-connectivity/

The WiTap sample from Apple demonstrates peer-to-peer networking over Wi-Fi and Bluetooth. Apple 的WiTap示例演示了通过 Wi-Fi 和蓝牙进行的点对点网络。 Using Bonjour, the application both advertises itself on the local network and displays a list of other instances on the network.使用 Bonjour,应用程序既可以在本地网络上宣传自己,又可以显示网络上其他实例的列表。 Supports infrastructure networks, peer-to-peer Bluetooth, and peer-to-peer Wi-Fi (on compatible hardware).支持基础设施网络、对等蓝牙和对等 Wi-Fi(在兼容硬件上)。

I have personally tested it and it works fine and well documented.我已经亲自测试过它,它运行良好并且有据可查。

I think socket.io is the best choice.我认为 socket.io 是最好的选择。 It is built on top of engine.io (which in turn is built on the fastest websocket implementation: ws ) It has oldest to newest fallbacks, so it starts with long polling and works its way up.它建立在 engine.io 之上(反过来又建立在最快的 websocket 实现上: ws )它具有从最旧到最新的回退,因此它从长轮询开始并逐步向上。 This guarantees a quick initial connection instead of needing to poll the device for features.这保证了快速的初始连接,而不需要轮询设备以获取功能。 You can read more on this here .您可以在此处阅读更多相关信息 Best of all, it handles everything seamlessly.最重要的是,它可以无缝处理所有事情。 You write your code as if websockets are supported on connecting devices and if not it will use other methods behind the scenes.您编写代码就好像连接设备支持 websockets 一样,如果不支持,它将在幕后使用其他方法。

This post details many of the websocket libraries you could use with your server.这篇文章详细介绍了您可以在服务器上使用的许多 websocket 库。 Which websocket library to use with Node.js? 哪个 websocket 库与 Node.js 一起使用?

I am convinced Bonjour is the best solution:我相信Bonjour是最好的解决方案:

Apps can also leverage Bonjour to automatically detect other instances of the app (or other services) on the network.应用程序还可以利用 Bonjour 自动检测网络上应用程序(或其他服务)的其他实例。

However I've never used it myself;但是我自己从来没有使用过它; perhaps someone who has can comment?也许有人可以发表评论?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM