Using CocoaHTTPServer to "send" a message

1.2k views Asked by At

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.

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.

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.

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"

1

There are 1 answers

3
Aaron Brager On BEST ANSWER

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.

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.

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.

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.