我正在使用ZMQ Stable Release 3.2.4并将其编译到iOS中 并且还使用IOS ZMQ Api。 它大部分时间都很完美。但是,有时在启动应用程序时,第一条消息丢失,接收方始终处于空闲状态。重启应用程序后,它再次运行良好。
是否需要在subSocket中设置以使其更稳定?
这是我的代码:
IP_ADDRESS = @"127.0.0.1";
NSString *kEndPoint = [NSString stringWithFormat:@"tcp://%@:%d", IP_ADDRESS, PUB_PORT];
self.pubSocket = [ctx socketWithType:ZMQ_PUB];
[self.pubSocket connectToEndpoint:kEndPoint];
NSString *rEndPoint = [NSString stringWithFormat:@"tcp://%@:%d", IP_ADDRESS, SUB_PORT];
self.subSocket = [ctx socketWithType:ZMQ_SUB];
[self.subSocket connectToEndpoint:rEndPoint];
sleep(1.5); // add sleep to make sure the socket is ready
[self registerListener:self forTopic:TOPIC_topic_resolution];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
pollitems[0].socket = [subSocket socket] ;
pollitems[0].events = ZMQ_POLLIN;
NSData * data = [subSocket receiveDataWithFlags:ZMQ_DONTWAIT];
// receive data and do logic here
});
答案 0 :(得分:2)
在我们使用zmq(libzmq)的iOS应用程序中,我通常设置超时以避免可怕的挂起永远等待接收...尝试使用这样的setockopt:
int timeout = 6000;
zmq_setsockopt(self->zmqSend, ZMQ_SNDTIMEO, &timeout, sizeof(int));
zmq_setsockopt(self->zmqSend, ZMQ_RCVTIMEO, &timeout, sizeof(int));
希望有所帮助!
JB