我有一个 Rails Web 应用程序,用于从运行我开发的应用程序的 iOS 设备收集数据。
Rails Web 服务器将处理与 Apple 推送通知服务的通信,因此 iOS 应用程序将向其发送所需的设备令牌和一些其他应用程序特定的数据。
Mac 应用程序将与 Rails 应用程序通信,以将消息推送到 iOS 设备。 Mac 应用程序和 Rails Web 应用程序都将在同一台 Mac 服务器上的同一用户帐户下运行。
iPhone 和 Mac 应用程序与 Rails Web 应用程序之间的通信将使用 Web 应用程序公开的 RESTful Web 服务来完成。
一旦 iOS 应用程序将其数据发送到 Rails Web 服务,我需要将该数据发送到将与 Rails Web 应用程序在同一服务器上运行的 Mac 应用程序,以便它知道需要推送哪些消息并他们应该被推到哪里。
如果我想在同一个盒子上的 Rails Web 应用程序和 Mac 应用程序之间进行通信,我应该寻找什么?
我研究过通过 RESTful 服务从 Mac 应用程序轮询 Web 应用程序,但我认为这不是最优雅或最有效的解决方案。有什么方法可以让我从 Ruby 调用 Mac 应用程序中的某些操作吗?
编辑:
我刚刚阅读了NSDistributedNotifications
和分布式通知中心。这听起来像是我正在寻找的。
是否可以使用 Rails 应用程序中的 RubyCocoa 发送分布式通知并让 Mac 应用程序侦听此通知?
I have a Rails web app that is used to gather data from an iOS device running an app I have developed.
The Rails web server will be handling communication with the Apple Push Notification service, so the iOS app will be sending it the required Device Token and some other application specific data.
The Mac app will be communicating with the Rails app to push a message to an iOS device. Both the Mac app and the Rails web app will be running on the same Mac server, under the same user account.
The communication between the the iPhone and Mac apps and the Rails web app will be done using the RESTful web services exposed by the web app.
Once the iOS app has sent its data to the Rails web service, I need that data to be sent to the Mac App that will be running on the same server as the Rails web app, so that it know which messages need to be pushed and where they should be pushed to.
What should I be looking for if I want to communicate between a Rails web app and a Mac app on the same box?
I have looked into polling the web app from the Mac app via a RESTful service, but I don't think this is the most elegant or efficient solution. Is there some way that I can invoke some action from the Mac app from Ruby?
Edit:
I've just read about NSDistributedNotifications
and the the Distributed Notification Center. This sounds like what I'm looking for.
Is it possible to use RubyCocoa from the Rails app to send a distributed notification and have the Mac app listen for this notification?
发布评论
评论(5)
我认为你可以让你的 cocoa 应用程序实现一个监听本地端口的服务器,比如 localhost:28888 ,当你的 Rails 应用程序从 iOS 应用程序接收数据时,你可以将其发送到 localhost:28888
I think you can make your cocoa app implement a server which listen to a local port like localhost:28888 , and while your rails application receiving data from your iOS app, you can just sent it to localhost:28888
也许不是最好的解决方案,但如果 Rails 应用程序可以将通知数据写入轻量级本地数据库或特定本地目录中的纯文本文件,并让您的 Mac 应用程序及时读取数据,则数据已读取可以删除或移动到另一个表/目录。这可能是一个临时解决方案,理想的解决方案应该是 Rob 提到的单个 Mac Web 应用程序。
Maybe not the best solution, but if the Rails app can write the notification data to a lightweight local database or just plain text files in a specific local directory, and let your Mac app to read the data in a timely fashion, data have been read can be deleted or moved to another table/directory. This could be a temp solution, the ideal solution should be a single Mac web app as Rob mentioned.
我在 OS X 上没有任何个人经验,所以对此持保留态度。
如果您的应用程序的主要对象符合 KVC/KVO,您可以将其属性公开给 AppleScript,并使用 Ruby 的脚本桥来更改 Mac 应用程序属性的值。
然后,您可以在应用程序内部监听这些属性并采取相应的行动。
如果应用程序是基于文档开发的,那么大部分 KVC/KVO 合规性和对 AppleScript 的暴露将是自动的。请参阅:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_implement/SAppsImplement.html#//apple_ref/doc/uid/20000037-BBCJEEEC
另一方面,如果应用程序开发为单窗口应用程序,您将必须自己实现脚本支持。
I do not have any personal experience with this on OS X, so take it with a grain of salt.
If your app's main objects are KVC/KVO compliant, you can expose its properties to AppleScript, and use Ruby's scripting bridge to change values of the Mac app's properties.
Then, internally in the app you can listen to those properties and act accordingly.
If the app is developed as Document-based, then much of the KVC/KVO compliance and exposure to AppleScript will be automatic. See this: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_implement/SAppsImplement.html#//apple_ref/doc/uid/20000037-BBCJEEEC
On the other hand, if the app is developed as a single-window app, you will have to implement the scripting support yourself.
也许传递“它们都在同一台服务器上且具有同一用户”并不是最好的主意,因为有时将来您可能必须对其进行扩展,然后您将不得不重写大量代码。
如果您继续使用 Restful api,您可以将所有内容移动到任何您想要的地方。
有很多方法可以实现这种IPC
例如,您可以拥有:套接字 - socket(2)、fifo 文件 - mkfifo(3)、共享内存 - shmget(2)
非常简单的解决方案是让 mac 应用程序侦听 udp 套接字并等待命令,
这里是 udp echo 的示例:
http://developer.apple.com/library/mac/#samplecode/UDPEcho/Listings/UDPEcho_m.html# //apple_ref/doc/uid/DTS40009660-UDPEcho_m-DontLinkElementID_5
更简单的是 mkfifo:
但我会推荐 udp 服务器或只坚持使用 Restful api,因为你可以根据需要缩放它(希望在你有数百万用户,你可以创建更多的 Rails 前端,并且所有这些前端都可以向 mac 服务器发送消息)
Probably it is not the best idea to relay on 'they are both on the same server with the same user' because sometimes in the future you might have to scale it, and then you will have to rewrite big chunks of code.
If you continue to use restful apis you can move everything wherever you want.
there are a lot of ways to do this kind of IPC tho
for example you can have: socket - socket(2), fifo file - mkfifo(3), shared memory - shmget(2)
very simple solution is to have the mac app listen on udp socket and wait for commands
here is an example for udp echo:
http://developer.apple.com/library/mac/#samplecode/UDPEcho/Listings/UDPEcho_m.html#//apple_ref/doc/uid/DTS40009660-UDPEcho_m-DontLinkElementID_5
even more simple is mkfifo:
but i would recommend udp server or just stick with the restful api, because you can scale it as much as you want (hopefully when you have millions of users you can create more rails front ends and all of them can send messages to the mac server)
我认为 NSDistributedNotifications 实现不会对您有帮助。可能潜在的概念在这里适用。您需要在 iPhone 上实现 REST 服务器才能检索数据。我不知道有现成的 API 方法可以做到这一点。
如果我说得对的话请纠正我!
I don't think the NSDistributedNotifications implementation will help you. May be the underlying concept is appliable here. You need to implement a REST-Server on the iPhone in order to retrieve data. I'm not aware of a ready made API way of doing so.
Please correct me if I'm right!