I searched allot as I am too implementing this type of app, at many places I found library that have many types of errors while implementing with iPhone application. I know you have solved your problem but this answer is for those who are still suffering. I combined libs of rabbitmq-c and obejective-c wrapper , erase issues and store it at my own place. You can have rabbitmq-lib for Objective-C development given link.
https://dl.dropboxusercontent.com/u/75870052/AMQPLib.zip
Include this library, import files as given below:-
#import "AMQPExchange.h"
#import "AMQPConsumer.h"
#import "AMQPConnection.h"
#import "AMQPConsumerThread.h"
#import "AMQPChannel.h"
#import "AMQPQueue.h"
#import "AMQPMessage.h"
Define your credentials, I have used default.
#define host @"localhost"
#define routingQueue @"CreateQueue"
#define port 5672
#define user @"guest"
#define pass @"guest"
For publishing message on server use following method.
- (IBAction)send:(id)sender {
NSError *error= nil;
NSError *error2 = nil;
NSError *error3 = nil;
NSError *error4 = nil;
AMQPConnection *connection = [[AMQPConnection alloc] init];
[connection connectToHost:host onPort:port error:&error];
if (error != nil){
NSLog(@"Error connection: %@", error);
return;
}
[connection loginAsUser:user withPasswort:pass onVHost:@"/" error:&error];
if (error != nil){
NSLog(@"Error logined: %@", error);
return;
}
AMQPChannel *channel = [connection openChannelError:&error2];
AMQPExchange *exchange = [[AMQPExchange alloc] initDirectExchangeWithName:@"AMQP" onChannel:channel isPassive:NO isDurable:NO getsAutoDeleted:NO error:&error];
if (error != nil){
NSLog(@"Error declareExchange: %@", error);
return;
}
AMQPQueue *queue = [[AMQPQueue alloc] initWithName:routingQueue onChannel:channel error:&error3];
if (error != nil){
NSLog(@"Error declare Queue: %@", error);
return;
}
BOOL success = [queue bindToExchange:exchange withKey:routingQueue error:&error4];
if (success) {
amqp_basic_properties_t props;
props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
props.content_type = amqp_cstring_bytes("text/plain");
props.delivery_mode = 2;
props.priority = 1;
//Here put your message to publish...
[exchange publishMessage:@"YOUR MESSAGE" usingRoutingKey:routingQueue propertiesMessage:props mandatory:NO immediate:NO error:&error];
if (error != nil){
NSLog(@"Error declareExchange: %@", error);
return;
}
}
}
For receiving message, you need delegate.
-(IBAction)receiveMessage:(id)sender
{
NSError *error= nil;
NSError *error2 = nil;
NSError *error3 = nil;
NSError *error4 = nil;
AMQPConnection *connection = [[AMQPConnection alloc] init];
[connection connectToHost:host onPort:port error:&error];
[connection loginAsUser:user withPasswort:pass onVHost:@"/" error:&error];
AMQPChannel *channel = [connection openChannelError:&error2];
AMQPQueue *queue = [[AMQPQueue alloc] initWithName:routingQueue onChannel:channel isPassive:NO isExclusive:NO isDurable:NO getsAutoDeleted:NO error:&error3];
AMQPConsumer *consumer = [[AMQPConsumer alloc] initForQueue:queue onChannel:&channel useAcknowledgements:YES isExclusive:NO receiveLocalMessages:NO error:&error4 deepLoop:1];
AMQPConsumerThread *consumerThread = [[AMQPConsumerThread alloc] initWithConsumer:consumer delegate:self nameThread:@"myThread" persistentListen:NO];
consumerThread.delegate=self;
[consumerThread start];
}
-(void)amqpConsumerThreadReceivedNewMessage:(AMQPMessage *)theMessage
{
NSLog(@"message = %@", theMessage.body);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…