GCDAsyncUdpSocket本身提供的:
- (void)sendData:(NSData *)data toHost:(NSString *)host port:(uint16_t)port withTimeout:(NSTimeInterval)timeout tag:(long)tag
方法写入参数timeout默认是不会根据填写的参数来超时的,需要在这个方法的
GCDAsyncUdpSendPacket *packet = [[GCDAsyncUdpSendPacket
alloc] initWithData:data
timeout:timeout tag:tag]; 下面添加这行代码就可以根据输入的参数来超时了
[self
setupSendTimerWithTimeout:timeout];
然后就会调用:
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError
*)error 委托方法了
还有本人为发送数据可以建立一个类,通过
[self
performSelector:@selector(requestTimeout)
withObject:nil
afterDelay:PACKET_TIMEOUT];
[NSObject
cancelPreviousPerformRequestsWithTarget:self
selector:@selector(requestTimeout)
object:nil];
或者定时器来也可以实现一个超时机制
时间: 2024-10-03 21:57:57