这是基于 [https://github.com/juretta/objc-stomp] 的一个简单的 STOMP 客户端,支持 Stomp v1.1 和 v1.2。
这个库是为了与 SocketRocket [https://github.com/square/SocketRocket] 一起使用而创建的。但应该可以与其它 WebSocket 库一起使用。
将 YCStompClient.{h,m} 添加到您的项目中。
MyExample.h
#import <Foundation/Foundation.h>
@class YCStompClient;
@protocol YCStompClientDelegate;
@interface MyExample : NSObject<YCStompClientDelegate> {
@private
YCStompClient *service;
}
@property(nonatomic, retain) YCStompClient *service;
@end
在 MyExample.m 中
#define kUsername @"USERNAME"
#define kPassword @"PASS"
#define kQueueName @"/topic/systemMessagesTopic"
[...]
-(void) aMethod {
YCStompClient *s = [[YCStompClient 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 YCStompClientDelegate
- (void)stompClientDidConnect:(YCStompClient *)stompService {
NSLog(@"stompServiceDidConnect");
}
- (void)stompClient:(YCStompClient *)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。
原始代码
此版本