0

Having an embedded HTTP Server in an iOS application, How can I respond to an HTTP Request with a multipart message? I'd like to asynchronously send JPEGs in a multipart response (MJPEG).

I've tried doing so using GCDWebServer, if it's not supported by it, any working alternative would be fine as long as it can also work on iOS.

Alon Amir
  • 4,913
  • 9
  • 47
  • 86
  • Maybe this implementation of Alamofire can help you: http://stackoverflow.com/questions/26121827/uploading-file-with-parameters-using-alamofire Alamofire docs can be found here: https://github.com/Alamofire/Alamofire – doruvil Apr 19 '16 at 13:59

1 Answers1

1

GCDWebServer has no built-in API to create multipart messages. You would need to write such code and output a NSData object, which then you wrap into a GCDWebServerDataResponse that you return to the HTTP client.

Pol
  • 3,848
  • 1
  • 38
  • 55
  • The way HTTP works, you can only send one response per request. – Pol Apr 18 '16 at 17:21
  • That's not technically accurate, you can send the response as chunks at different times (such that it suspend the "first" chunk from closing the request) -> for the same request. Motion-JPEG takes advantage of it, see for example https://en.wikipedia.org/wiki/Motion_JPEG – Alon Amir Apr 18 '16 at 17:52
  • Thanks for letting me know it's not directly supported by GCDWebServer, and for creating this useful library :) would be nice if a solution comes up though. – Alon Amir Apr 18 '16 at 20:41
  • No, no, it's technically correct. Just because you send a response in multiple chunks, doesn't make it multiple responses :) – Pol Apr 19 '16 at 16:30
  • Note that GCDWebServer does support chunk content encoding for requests and responses, which `GCDWebServerStreamedResponse` leverages. – Pol Apr 19 '16 at 16:32
  • Heh, well, yeah, that's what I'm trying to achieve :D. So is `GCDWebServerStreamedResponse` capable of emitting multiple chunks of data at different times (possibly an indeterminate amount of chunks)? I'm basically forwarding jpegs using an MPEG chunked stream to a browser's `video` tag. obviously I can't guess when the stream ends, or how many jpegs I have to forward, since It has to be like "real time" video. Is there any documentation on how to do something similar using `GCDWebServerStreamedResponse`? – Alon Amir Apr 19 '16 at 16:46
  • There's no documentation for this, you're going to be on your own. Note that if you don't transmit any data for 30-60s, it's likely the browser will close the connection thinking it's stalled. – Pol Apr 20 '16 at 00:08