long polling in objective-C

前端 未结 2 1357
心在旅途
心在旅途 2020-11-30 22:25

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 v

相关标签:
2条回答
  • 2020-11-30 22:36

    Long polling is making a read request to a server, the server gets the requests, finds that there's nothing of interest to send you, and rather than returning nothing, or "empty", it instead holds on to the request until something interesting shows up. Once it finds something, it writes to the socket and the client receives the data.

    The detail is that for this entire time, using generic socket programming, the client is blocked and hanging on the sockets read call.

    There are two ways to deal with this (well, three if you don't mind being stuck on the main thread for several seconds, but let's not count that one).

    1. Put the socket handling code in a thread. In this case, the entire socket process is in an independent thread within the program, so it happily sits stuck on the read waiting for a response.

    2. Use asynchronous socket handling. In this case, your socket read does NOT block the main thread. Instead, you pass in call back functions that respond to the activity on the socket, and then go about your merry way. On the Mac, there's CFSocket which exposes this kind of functionality. It spawns its own thread, and manages the socket connection using select(2).

    This is a nice little post talking about CFSocket.

    CFSocket fits well in to the Mac idiom of message passing and eventing, and is probably what you should be looking at for doing this kind of work. There is also an Obj-C class wrapper built on CFSocket called ULNetSocket (formerly NetSocket).

    0 讨论(0)
  • 2020-11-30 22:49

    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.

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