这是一个简单的基于 [https://github.com/juretta/objc-stomp] 的STOMP客户端,支持Stomp v1.1和v1.2。
这个库是为与SocketRocket [https://github.com/square/SocketRocket] 一起使用而创建的。但它应该可以与其它WebSocket库一起工作。
将CRVStompClient.{h,m}添加到项目中。
MyExample.h
#import <Foundation/Foundation.h>
@class CRVStompClient;
@protocol CRVStompClientDelegate;
@interface MyExample : NSObject<CRVStompClientDelegate> {
@private
CRVStompClient *service;
}
@property(nonatomic, retain) CRVStompClient *service;
@end
在MyExample.m中
#define kUsername @"USERNAME"
#define kPassword @"PASS"
#define kQueueName @"/topic/systemMessagesTopic"
[...]
-(void) aMethod {
CRVStompClient *s = [[CRVStompClient alloc]
initWithHost:@"localhost"
port:61613
login:kUsername
passcode:kQueueName
delegate:self];
[s connect];
NSDictionary *headers = [NSDictionary dictionaryWithObjectsAndKeys:
@"client", @"ack",
@"true", @"activemq.dispatchAsync",
@"1", @"activemq.prefetchSize", nil];
[s subscribeToDestination:kQueueName withHeader: headers];
[self setService: s];
[s release];
}
#pragma mark CRVStompClientDelegate
- (void)stompClientDidConnect:(CRVStompClient *)stompService {
NSLog(@"stompServiceDidConnect");
}
- (void)stompClient:(CRVStompClient *)stompService messageReceived:(NSString *)body withHeader:(NSDictionary *)messageHeader {
NSLog(@"gotMessage body: %@, header: %@", body, messageHeader);
NSLog(@"Message ID: %@", [messageHeader valueForKey:@"message-id"]);
// If we have successfully received the message ackknowledge it.
[stompService ack: [messageHeader valueForKey:@"message-id"]];
}
- (void)dealloc {
[service unsubscribeFromDestination: kQueueName];
[service release];
[super dealloc];
}
在iOS5+上运行良好,支持ARC。
原始代码
此版本