Long polling with NSURLConnection

前端 未结 5 1373
天涯浪人
天涯浪人 2020-12-31 06:46

I\'m working on an iPhone application which will use long-polling to send event notifications from the server to the client over HTTP. After opening a connection on the ser

相关标签:
5条回答
  • 2020-12-31 07:11

    I ran into this today. I wrote my own class to handle this, which mimics the basic functionality of NSURLConnection.

    http://github.com/nall/SZUtilities/blob/master/SZURLConnection.h

    0 讨论(0)
  • 2020-12-31 07:14

    There is another HTTP API Implementation named ASIHttpRequest. It doesn't have the problem stated above and provides a complete toolkit for almost every HTTP feature, including File Uploads, Cookies, Authentication, ...

    http://allseeing-i.com/ASIHTTPRequest/

    0 讨论(0)
  • 2020-12-31 07:15

    NSURLConnection will buffer the data while it is downloading and give it all back to you in one chunk with the didReceiveData method. The NSURLConnection class can't tell the difference between network lag and an intentional split in the data.

    You would either need to use a lower-level network API like CFSocket as you mention (you would have access to each byte as it comes in from the network interface, and could distinguish the two parts of your payload), or you could take a look at a library like CURL and see what types of output buffering/non-buffering there is there.

    0 讨论(0)
  • 2020-12-31 07:22

    It sounds as if you need to flush the socket on the server-side, although it's really difficult to say for sure. If you can't easily change the server to do that, then it may help to sniff the network connection to see when stuff is actually getting sent from the server.

    You can use a tool like Wireshark to sniff your network.

    Another option for seeing what's getting sent/received to/from the phone is described in the following article:

    http://blog.jerodsanto.net/2009/06/sniff-your-iphones-network-traffic/

    Good luck!

    0 讨论(0)
  • 2020-12-31 07:29

    We're currently doing some R&D to port our StreamLink comet libraries to the iPhone.

    I have found that in the emulator you will start to get didReceiveData callbacks once 1KB of data is received. So you can send a junk 1KB block to start getting callbacks. It seems that on the device, however, this doesn't happen. In safari (on device) you need to send 2KB, but using NSURLConnection I too am getting no callbacks. Looks like I may have to take the same approach.

    I might also play with multipart-replace and some other more novel headers and mime types to see if it helps stimulate NSURLConnection.

    0 讨论(0)
提交回复
热议问题