gpt4 book ai didi

iphone - 从 CocoaAsyncSocket 读取数据

转载 作者:行者123 更新时间:2023-11-29 04:18:33 27 4
gpt4 key购买 nike

我正在使用 CocoaAsyncSocket ,我需要创建一个向服务器发送消息并等待服务器回复的函数,在委托(delegate)方法中它确实收到服务器响应,但我需要发送消息的函数等待服务器回复并返回响应。

- (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;
}

最佳答案

如果您需要同步发送某些内容,为什么不构建请求并使用 NSURLConnection api,如下所示:

NSData* data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];   

这将阻塞,直到您得到响应。

如果您想继续使用异步套接字方法,但强制使其成为同步调用,您可以通过添加以下方法来实现:

@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;
}

现在让你的方法像这样

- (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;
}

现在在你的CocoaAsyncSocket回调socket:didReadData:withTag:和socket:didDisconnectWithError:中确保你调用

[self signalCondition];

一旦调用,等待方法就会继续,并且您刚刚将异步调用变为同步。

关于iphone - 从 CocoaAsyncSocket 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13323231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com