Reputation: 9867
I am using CocoaAsyncSocket , I need to make a function which sends a message to the server and wait until the server replies, in delegate methods it does receive server response but I need the function that is sending the message wait for the server to reply and return response.
- (NSString *)sendMessage:(NSString *)message{
NSError *err = nil;
if (![socket connectToHost:@"192.168.1.20" onPort:52523 error:&err]) // Asynchronous!
{
NSLog(@"Error is : %@", err);
}
[socket readDataWithTimeout:-1 tag:1];
NSData* data =[message dataUsingEncoding:NSUTF8StringEncoding];
[socket writeData:data withTimeout:-1 tag:1];
NSString *xmlString = [[NSString alloc] init];
// Here I need the function wait and receive the response
return xmlString;
}
Upvotes: 1
Views: 1203
Reputation: 1725
If you need to send something synchronously why not build a request and use NSURLConnection api like here:
NSData* data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
This will block until you get a response.
If you want to keep using the async socket approach but forcibly make it a synchronous call you can do this by adding the following methods:
@property (nonatomic, assign) BOOL com_private_condition;
@property (nonatomic, assign) NSThread* com_private_theWaitingThread;
...
@synthesize com_private_condition;
@synthesize com_private_theWaitingThread;
...
- (BOOL)waitForConditionWithTimeout:(NSTimeInterval)aTimeout
{
self.com_private_condition = NO;
self.com_private_theWaitingThread = [NSThread currentThread];
NSDate* theStartDate = [NSDate date];
NSDate* theEndDate = [NSDate dateWithTimeIntervalSinceNow:aTimeout];
do
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:theEndDate];
NSTimeInterval theElapsedTime = -[theStartDate timeIntervalSinceNow];
if (theElapsedTime >= aTimeout)
{
return NO;
}
if (self.com_private_condition)
{
return YES;
}
} while (YES);
}
- (void)signalCondition
{
[self performSelector:@selector(com_private_signalCondition:)
onThread:self.com_private_theWaitingThread
withObject:nil waitUntilDone:NO];
}
- (void)com_private_signalCondition:(id)aParam
{
self.com_private_condition = YES;
}
Now make your method like this
- (NSString *)sendMessage:(NSString *)message{
NSError *err = nil;
if (![socket connectToHost:@"192.168.1.20" onPort:52523 error:&err]) // Asynchronous!
{
NSLog(@"Error is : %@", err);
}
[socket readDataWithTimeout:-1 tag:1];
NSData* data =[message dataUsingEncoding:NSUTF8StringEncoding];
[socket writeData:data withTimeout:-1 tag:1];
//make xmlString a variable on your class and set it in your async socket callback when you get the data. once the wait is over just return it.
//NSString *xmlString = [[NSString alloc] init];
// Here I need the function wait and receive the response
//call read and then wait
[socket readDataWithTimeout:-1 tag:1];
[self waitForConditionWithTimeout:9999.0]; //or some other wait timeout of your choosing
//this will block until [self signalCondition] is called by your socket callbacks.
return self.xmlString;
}
Now in your CocoaAsyncSocket callback socket:didReadData:withTag: and socket:didDisconnectWithError: make sure you call
[self signalCondition];
Once you call that the waiting method will continue, and you've just make an asynchronous call synchronous.
Upvotes: 2