MMPReactiveStompClient 0.2.1

MMPReactiveStompClient 0.2.1

测试已测试
语言语言 Obj-CObjective C
许可协议 MIT
发布上次发布2015年3月

Mamad Purbo维护。



 
依赖项
ReactiveCocoa>= 0
SocketRocket>= 0
 

MMPReactiveStompClient是一个基于ReactiveCocoaSocketRocket的响应式WebSocket/STOMP客户端库。STOMP的实现基于StompKit

这是一个非常早期的版本,目前仅支持原始WebSocket、原始STOMP帧和消息以及基本的STOMP订阅信号。

安装

使用方法

以下代码展示了如何订阅原始WebSocket信号

#import "MMPReactiveStompClient.h"
#import <SocketRocket/SRWebSocket.h>
#import <ReactiveCocoa/ReactiveCocoa.h>

// connecting to a WebSocket server
stompClient = [[MMPReactiveStompClient alloc] initWithURL:[NSURL URLWithString:@"ws://:8080/stream/connect"]];

// opening the STOMP client returns a raw WebSocket signal that you can subscribe to
[[stompClient open]
    subscribeNext:^(id x) {
        if ([x class] == [SRWebSocket class]) {
            // First time connected to WebSocket, receiving SRWebSocket object
            NSLog(@"web socket connected with: %@", x);            
        } else if ([x isKindOfClass:[NSString class]]) {
            // Subsequent signals should be NSString
        }
    }
    error:^(NSError *error) {
        NSLog(@"web socket failed: %@", error);
    }
    completed:^{
        NSLog(@"web socket closed");
    }];

以下示例展示了如何获取STOMP帧、消息并订阅特定频道

// subscribe to raw STOMP frames
[[_stompClient stompFrames]
    subscribeNext:^(MMPStompFrame *frame) {
        NSLog(@"STOMP frame received: command = %@", frame.command);
    }];

// subscribe to any STOMP messages
[[stompClient stompMessages]
    subscribeNext:^(MMPStompMessage *message) {
        NSLog(@"STOMP message received: body = %@", message.body);
    }];

// subscribe to a STOMP destination
[[stompClient stompMessagesFromDestination:@"/topic/test"]
    subscribeNext:^(MMPStompMessage *message) {
        NSLog(@"STOMP message received: body = %@", message.body);
    }];

设置

在初始化后,您可以链式调用设置方法来指定自定义设置。以下各节将解释可用的设置。

SockJS

当使用SockJS服务器时,使用以下示例中的useSockJs

MMPReactiveStompClient *stompClient = [[[MMPReactiveStompClient alloc]
                                                                initWithURL:[NSURL URLWithString:@"ws://:8080/stream/connect"]]
                                                                useSockJs];

自定义STOMP订阅ID

默认情况下,将每个对STOMP目的地的订阅都会分配一个具有以下模式的ID

sub-[running_number]

其中[running_number]是一个从0开始自动递增的数字。如果您需要生成一个自定义ID,您可以实现MMPStompSubscriptionIdGenerator协议并将类传递给以下示例中的subscriptionIdGenerator方法。

// class implementing MMPStompSubscriptionIdGenerator protocol

@interface GeocoreStompSubscriptionIdGenerator : NSObject<MMPStompSubscriptionIdGenerator>

@property (nonatomic, assign) NSUInteger counter;

@end

@implementation GeocoreStompSubscriptionIdGenerator

// implement method for generating next ID for new subscription
- (NSString *)generateId {
    return [NSString stringWithFormat:@"mycustomsubid-%lu", (unsigned long)++_counter];
}

@end

作者

Mamad Purbo,[email protected]

许可协议

MMPReactiveStompClient在MIT许可下可用。有关更多信息,请参阅LICENSE文件。