Long polling with NSURLConnection

自古美人都是妖i 提交于 2019-11-30 08:31:33

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.

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

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!

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.

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!