目标C中的长轮询 [英] long polling in objective-C

查看:130
本文介绍了目标C中的长轮询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用API​​在网站上获取实时更新的应用程序。他们使用他们所谓的长轮询技术



< blockquote>

长轮询是
传统轮询技术的变体,
允许模拟从服务器到客户端的信息
。使用
长轮询,客户端以
的方式从服务器请求
信息,类似于正常轮询。但是,如果服务器没有可用于客户端的任何
信息,

而不是发送空响应,
服务器保留请求并等待
一些可用的信息。
一旦信息变为可用
(或在适当的超时后),
完整响应将被发送到
客户端。客户端通常然后
立即从服务器重新请求信息
,这样服务器
几乎总是有一个可用的
等待请求,它可以用来
响应事件传递数据。
在web / AJAX上下文中,长轮询是
,也称为Comet编程。



长轮询本身不是推送
技术,但可以在
的情况下使用,实际推送不是


基本上这是强制执行在收到回复后再向服务器发出请求。在iPhone应用程序中执行此操作的最佳方法是什么?这最终必须在后台运行。

解决方案

这正是的用例NSURLConnection sendSynchronousRequest 非常适合:

   - (void)longPoll {
//创建线程的自动释放池
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//撰写请求
NSError * error = nil;
NSURLResponse * response = nil;
NSURL * requestUrl = [NSURL URLWithString:@http://www.mysite.com/pollUrl];
NSURLRequest * request = [NSURLRequest requestWithURL:requestUrl];

//发送请求(将阻止直到响应回来)
NSData * responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:& response error:& error] ;

//将响应传递给处理程序(如果需要,还可以在这里检查错误)
[self performSelectorOnMainThread:@selector(dataReceived :)
withObject:responseData waitUntilDone:YES];

//清除游泳池
[游泳池排水];

//发送下一轮询请求
[self performSelectorInBackground:@selector(longPoll)withObject:nil];
}

- (void)startPoll {
//本例未涵盖:停止投票或确保在任何给定时间只有1个投票活动
[self performSelectorInBackground:@ selector(longPoll)withObject:nil];
}

- (void)dataReceived:(NSData *)theData {
//在此处理响应
}

或者,你可以使用异步I / O和委托回调来完成同样的事情,但在这种情况下,这确实很愚蠢。 / p>

I have an application that uses an API to get real time updates on the website. They use what they call a long-polling technique:

Long polling is a variation of the traditional polling technique and allows emulation of an information push from a server to a client. With long polling, the client requests information from the server in a similar way to a normal poll. However, if the server does not have any information available for the client, instead of sending an empty response, the server holds the request and waits for some information to be available. Once the information becomes available (or after a suitable timeout), a complete response is sent to the client. The client will normally then immediately re-request information from the server, so that the server will almost always have an available waiting request that it can use to deliver data in response to an event. In a web/AJAX context, long polling is also known as Comet programming.

Long polling is itself not a push technology, but can be used under circumstances where a real push is not possible.

Basically this enforces to make a request back to the server once you've got a response back. What is the best way to do this in an iphone application? This eventually has to run in the background.

解决方案

This is exactly the sort of use-case that NSURLConnection sendSynchronousRequest is perfect for:

- (void) longPoll {
    //create an autorelease pool for the thread
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    //compose the request
    NSError* error = nil;
    NSURLResponse* response = nil;
    NSURL* requestUrl = [NSURL URLWithString:@"http://www.mysite.com/pollUrl"];
    NSURLRequest* request = [NSURLRequest requestWithURL:requestUrl];

    //send the request (will block until a response comes back)
    NSData* responseData = [NSURLConnection sendSynchronousRequest:request
                            returningResponse:&response error:&error];

    //pass the response on to the handler (can also check for errors here, if you want)
    [self performSelectorOnMainThread:@selector(dataReceived:) 
          withObject:responseData waitUntilDone:YES];

    //clear the pool 
    [pool drain];

    //send the next poll request
    [self performSelectorInBackground:@selector(longPoll) withObject: nil];
}

- (void) startPoll {
    //not covered in this example:  stopping the poll or ensuring that only 1 poll is active at any given time
    [self performSelectorInBackground:@selector(longPoll) withObject: nil];
}

- (void) dataReceived: (NSData*) theData {
    //process the response here
}

Alternately, you could use async I/O and delegate callbacks to accomplish the same thing, but that would really be kind of silly in this case.

这篇关于目标C中的长轮询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆