MPMessagePack, Objective C/msgpack. org [Objective C ]的MessagePack实现

分享于 

5分钟阅读

GitHub

  繁體 雙語
MessagePack implementation for Objective-C / msgpack.org[Objective-C]
  • 源代码名称:MPMessagePack
  • 源代码网址:http://www.github.com/gabriel/MPMessagePack
  • MPMessagePack源代码文档
  • MPMessagePack源代码下载
  • Git URL:
    git://www.github.com/gabriel/MPMessagePack.git
    Git Clone代码到本地:
    git clone http://www.github.com/gabriel/MPMessagePack
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/gabriel/MPMessagePack
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    MPMessagePack

    MessagePack插件框架。

    MessagePack是一种高效的二进制序列化格式。 它允许你在多种语言之间交换数据,比如 JSON。 但它更快更小。

    Podfile
    pod "MPMessagePack"
    Cartfile
    
    github"gabriel/MPMessagePack"
    
    
    
    
    MPMessagePack

    #import<MPMessagePack/MPMessagePack.h>NSDictionary *dict =
     @{
     @"n": @(32134123),
     @"bool": @(YES),
     @"array": @[@(1.1f), @(2.1)],
     @"body": [NSDatadata],
     };NSData *data = [dict mp_messagePack];

    或者通过 MPMessagePackWriter

    NSError *error = nil;NSData *data = [MPMessagePackWriter writeObject:dict error:&error];

    如果你需要使用有序字典。

    MPOrderedDictionary *dict = [[MPOrderedDictionary alloc] init];
    [dict addEntriesFromDictionary:@{@"c": @(1), @"b": @(2), @"a": @(3)}];
    [dict sortKeysUsingSelector:@selector(localizedCaseInsensitiveCompare:)];NSData *data = [dict mp_messagePack];

    id obj = [MPMessagePackReader readData:data error:&error];
    MPMessagePackReader *reader = [[MPMessagePackReader alloc] initWithData:data];id obj1 = [reader read:&error]; // Read an objectid obj2 = [reader read:&error]; // Read another object
    RPC

    请参见 msgpack-rpc

    它还支持帧选项,其中将以字节为( 作为一个 msgpack'的字节数前缀给rpc消息。

    客户端

    请求带完成块:

    MPMessagePackClient *client = [[MPMessagePackClient alloc] init];
    [client openWithHost:@"localhost"port:93434completion:^(NSError *error) {
     // If error we failed [client sendRequestWithMethod:@"test"params:@[@{@"arg": @(1)}] completion:^(NSError *error, id result) {
     // If error we failed// Otherwise the result }];
    }];

    你还可以同步请求:

    NSError *error = nil;id result = [client sendRequestWithMethod:@"test"params:@[@{@"arg": @(1)}] messageId:3timeout:5.0error:&error];// error.code == MPRPCErrorRequestTimeout on timeout

    并取消进行的请求:

    BOOL cancelled = [client cancelRequestWithMessageId:3];// cancelled == YES, if the request was in progress and we cancelled it

    电子邮件服务器

    MPMessagePackServer *server = [[MPMessagePackServer alloc] initWithOptions:MPMessagePackOptionsFramed];
    server.requestHandler = ^(NSString *method, id params, MPRequestCompletion completion) {
     if ([method isEqualToString:@"echo"]) {
     completion(nil, params);
     } else {
     completion(@{@"error": {@"description": @"Method not found"}}, nil);
     }
    };NSError *error = nil;if (![server openWithPort:93434error:&error]) {
     // Failed to open}

    Mantle编码

    如果使用地幔将对象编码为 JSON ( 然后是 msgpack ),则可以为MPMessagePackClient指定编码器:

    @interfaceKBMantleCoder : NSObject <MPMessagePackCoder>@end@implementationKBMantleCoder- (NSDictionary *)encodeModel:(id)obj {
     return [obj conformsToProtocol:@protocol(MTLJSONSerializing)]? [MTLJSONAdapter JSONDictionaryFromModel:obj] : obj;
    }@end

    然后在客户机中:

    MPMessagePackClient *client = [[MPMessagePackClient alloc] init];
    client.coder = [[KBMantleCoder alloc] init];

    XPC

    有一个实验,但功能的msgpack-rpc超过 XPC ( 请参见XPC目录)。 更多详细信息。


    相关文章