I am using CocoaHTTPServer in a MacOSX app (Server) that serves up a local directory of images. I have a corresponding iOS app (Client) that uses AFHTTPRequestOperation (AFNetworking) to retrieve an image file from the Mac over the local network. This is working well.
我在MacOSX应用程序(服务器)中使用CocoaHTTPServer,它提供本地图像目录。我有一个对应的iOS应用程序(客户端),它使用AFHTTPRequestOperation (AFNetworking)从Mac上从本地网络检索图像文件。这是工作得很好。
What I would like to do next is, when a user chooses a specific image in the Mac app the iPad app is notified to download that image and display it.
接下来我想做的是,当用户在Mac应用程序中选择一个特定的图像时,iPad应用程序会被通知下载并显示该图像。
The method I currently employ is to serve a simple imageToDisplay.txt file that includes the file name of the image to display. The iPad app is constantly polling this file, and if the filename changes it downloads it, etc. It works but seems clunky. I have thought of implementing a GET method on the server that would also return the filename. This approach would still require polling by the client.
我目前使用的方法是提供一个简单的imageToDisplay。txt文件,其中包含要显示的图像的文件名。iPad应用程序不断地轮询这个文件,如果文件名改变了它下载它,等等。它可以工作,但是看起来很笨重。我想在服务器上实现一个GET方法,它也会返回文件名。这种方法仍然需要客户端轮询。
Is there a more elegant way to trigger the download (without polling) with the pieces I already have in place? Essentially sending a message from the server to the client - "download image27.jpg now"
是否有一种更优雅的方式来触发下载(没有轮询),使用我已有的片段?本质上,从服务器向客户端发送一条消息——“立即下载image27.jpg”
1 个解决方案
#1
1
WebSockets (SocketRocket)
There are a few ways you could implement this. As noted in the comments, WebSockets is one. The most robust freely available WebSockets library for iOS is SocketRocket (which rhymes). There is ample sample (also rhymes) code on the page I just linked to, so I won't include any here.
有几种方法可以实现这一点。正如在评论中提到的,WebSockets就是其中之一。iOS中最健壮的免费WebSockets库是SocketRocket(押韵)。在我链接到的页面上有足够的示例(也有韵律)代码,所以我不会在这里添加任何代码。
AFNetworking
Since you're using AFNetworking already, you could also take a look at the new AFNetworking 2.0 (to be released soon), which includes support for realtime networking via Rocket.
由于您已经在使用AFNetworking了,您还可以查看新的AFNetworking 2.0(即将发布),其中包括通过Rocket来支持实时网络。
This would allow your app to maintain an open connection, and would look something like this:
这将使你的应用程序保持一个开放的连接,并且看起来像这样:
[client SUBSCRIBE:@"/currentImage" usingBlock:^(NSArray *operations, NSError *error) {
for (AFJSONPatchOperation *operation in operations) {
switch (operation.type) {
case AFJSONReplaceOperationType:
// replace old image with new image
break;
default:
break;
}
}
} error:nil];
As long as your client doesn't cancel, it will continue to receive updates from the server whenever they happen.
只要您的客户端不取消,它将继续从服务器接收更新,无论何时发生。
Your server would need to send data in the appropriate format, and there's an experimental branch of Rack::Scaffold that does this.
您的服务器需要以适当的格式发送数据,并且有一个实验性的Rack::Scaffold可以做到这一点。
Notes
These approaches might be overkill if you're only changing your image once a week; in that case you should cache an image for some reasonable time period.
如果你每周只改变一次自己的形象,这些方法可能会显得有些过头;在这种情况下,您应该在合理的时间内缓存映像。
#1
1
WebSockets (SocketRocket)
There are a few ways you could implement this. As noted in the comments, WebSockets is one. The most robust freely available WebSockets library for iOS is SocketRocket (which rhymes). There is ample sample (also rhymes) code on the page I just linked to, so I won't include any here.
有几种方法可以实现这一点。正如在评论中提到的,WebSockets就是其中之一。iOS中最健壮的免费WebSockets库是SocketRocket(押韵)。在我链接到的页面上有足够的示例(也有韵律)代码,所以我不会在这里添加任何代码。
AFNetworking
Since you're using AFNetworking already, you could also take a look at the new AFNetworking 2.0 (to be released soon), which includes support for realtime networking via Rocket.
由于您已经在使用AFNetworking了,您还可以查看新的AFNetworking 2.0(即将发布),其中包括通过Rocket来支持实时网络。
This would allow your app to maintain an open connection, and would look something like this:
这将使你的应用程序保持一个开放的连接,并且看起来像这样:
[client SUBSCRIBE:@"/currentImage" usingBlock:^(NSArray *operations, NSError *error) {
for (AFJSONPatchOperation *operation in operations) {
switch (operation.type) {
case AFJSONReplaceOperationType:
// replace old image with new image
break;
default:
break;
}
}
} error:nil];
As long as your client doesn't cancel, it will continue to receive updates from the server whenever they happen.
只要您的客户端不取消,它将继续从服务器接收更新,无论何时发生。
Your server would need to send data in the appropriate format, and there's an experimental branch of Rack::Scaffold that does this.
您的服务器需要以适当的格式发送数据,并且有一个实验性的Rack::Scaffold可以做到这一点。
Notes
These approaches might be overkill if you're only changing your image once a week; in that case you should cache an image for some reasonable time period.
如果你每周只改变一次自己的形象,这些方法可能会显得有些过头;在这种情况下,您应该在合理的时间内缓存映像。