gom-client-objc 0.6.0

gom-client-objc 0.6.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2014 年 12 月

Julian Krumow 维护。



  • 作者
  • Julian Krumow

Objective-C GOM 客户端

要求

在您的应用程序项目中使用 GOM 客户端

要在您的项目中使用 Objective-C GOM 客户端,请在 Podfile 中添加以下行:

pod "gom-client-objc"

并使用 CocoaPods 依赖管理器安装所有必要的依赖项。

所有依赖项都在 gom-client-objc.podspec 文件中定义。

使用方法

初始化

NSURL *gomURI = [NSURL URLWithString:@"http://<ip-or-name>:<port>"];
GOMClient *gomClient = [[GOMClient alloc] initWithGomURI:gomURI delegate:self];

GOMClient 对象初始化和设置完成后,它将通过 GOMClientDelegate 协议的 - (void)gomClientDidBecomeReady:(GOMClient *)gomClient 方法返回一个对 GOMClient 对象的引用。

错误处理

在 GOM 请求过程中发生的错误将通过相应方法的完成块传递给发送方。

基本错误通过 GOMClientDelegate 协议的 - (void)gomClient:(GOMClient *)gomClient didFailWithError:(NSError *)error 方法返回到代理。

RESTful 操作

  • GET/检索

    • 属性检索
    [gomClient retrieve:@"/tests/node_1:attribute_1" completionBlock:^(NSDictionary *response, NSError *error) {
    
        // Your code here
    
    }];
    {attribute = {
        ctime = "2013-12-29T17:48:52+01:00";
        mtime = "2013-12-29T17:48:52+01:00";
        name = "attribute_1";
        node = "/tests/node_1";
        type = string;
        value = 100;
    }}
    
    
    • 检索不存在的属性
    NSError Domain=de.artcom.gom-client-objc Code=404 "not found"
    
    • 节点检索
    [gomClient retrieve:@"/tests/node_1" completionBlock:^(NSDictionary *response, NSError *error) {
    
        // Your code here
    
     }];
     {node = {
        ctime = "2013-12-29T17:49:07+01:00";
        entries = (
            {
                attribute = {
                    ctime = "2013-12-29T17:48:52+01:00";
                    mtime = "2013-12-29T17:48:52+01:00";
                    name = "attribute_1";
                    node = "/tests/node_1";
                    type = string;
                    value = 100;
                };
            },
            {
                attribute = {
                    ctime = "2013-12-29T17:49:00+01:00";
                    mtime = "2013-12-29T17:49:00+01:00";
                    name = "attribute_2";
                    node = "/tests/node_1";
                    type = string;
                    value = 20;
                };
            },
            {
                attribute = {
                    ctime = "2013-12-29T17:49:07+01:00";
                    mtime = "2013-12-29T17:49:07+01:00";
                    name = "attribute_3";
                    node = "/tests/node_1";
                    type = string;
                    value = 50;
                };
            }
        );
        mtime = "2013-12-29T17:49:07+01:00";
        uri = "/tests/node_1";
    }}
    
    • 检索不存在的节点
    NSError Domain=de.artcom.gom-client-objc Code=404 "not found"
    
  • POST/创建

    • 创建空节点
    gomClient create:@"/tests/node_1/test" withAttributes:nil completionBlock:^(NSDictionary *response, NSError *error) {
    
        // Your code here
    
    }];
    {node = {
        ctime = "2013-12-29T17:54:16+01:00";
        entries = (
        );
        mtime = "2013-12-29T17:54:16+01:00";
        uri = "/tests/node_1/test/75d4fb2d-6b4d-4bc0-9e12-91817f90da1d";
    }}
    
    • 创建带有属性的节点
    NSDictionary *attributes = @{@"attribute1": @"value1", @"attribute2" : @"value2", @"attribute3" : @"value3"};
    gomClient create:@"/tests/node_1/test" withAttributes:attributes completionBlock:^(NSDictionary *response, NSError *error) {
    
        // Your code here
    
    }];
    {node = {
        ctime = "2013-12-29T17:56:04+01:00";
        entries = (
            {
                attribute = {
                    ctime = "2013-12-29T17:56:04+01:00";
                    mtime = "2013-12-29T17:56:04+01:00";
                    name = attribute1;
                    node = "/tests/node_1/test/b382502c-6732-46ae-bef4-31d9d77ad97b";
                    type = string;
                    value = value1;
                };
            },
            {
                attribute = {
                    ctime = "2013-12-29T17:56:04+01:00";
                    mtime = "2013-12-29T17:56:04+01:00";
                    name = attribute2;
                    node = "/tests/node_1/test/b382502c-6732-46ae-bef4-31d9d77ad97b";
                    type = string;
                    value = value2;
                };
            },
            {
                attribute = {
                    ctime = "2013-12-29T17:56:04+01:00";
                    mtime = "2013-12-29T17:56:04+01:00";
                    name = attribute3;
                    node = "/tests/node_1/test/b382502c-6732-46ae-bef4-31d9d77ad97b";
                    type = string;
                    value = value3;
                };
            }
        );
        mtime = "2013-12-29T17:56:04+01:00";
        uri = "/tests/node_1/test/b382502c-6732-46ae-bef4-31d9d77ad97b";
    }}
    
  • PUT/更新

    • 属性更新
    [gomClient updateAttribute:@"/tests/node_1:attribute_1" withValue:@"50" completionBlock:^(NSDictionary *response, NSError *error) {
    
        // Your code here
    
    }];        
    {status = 200}
    
    • 节点更新
    NSDictionary *attributes = @{@"attribute1": @"100", @"attribute2" : @"200", @"attribute3" : @"300"};
    [gomClient updateNode:@"/tests/node_1/test/b382502c-6732-46ae-bef4-31d9d77ad97b" withAttributesValue:attributes completionBlock:^(NSDictionary *response, NSError *error) {
    
        // Your code here
    
    }];
    {status = 200}
    
  • DELETE/销毁

    • 销毁现有属性
    [gomClient destroy:@"/tests/node_1:attribute_3" completionBlock:^(NSDictionary *response, NSError *error) {
    
        // Your code here
    
    }];
    {"success" = 1}
    
    • 销毁现有节点
    [gomClient destroy:@"/tests/node_1" completionBlock:^(NSDictionary *response, NSError *error) {
    
        // Your code here
    
    }];
    {"success" = 1}
    
    • 销毁不存在的属性
    [gomClient destroy:@"/tests/node_1:attribute_x" completionBlock:^(NSDictionary *response, NSError *error) {
    
        // Your code here
    
    }];
    NSError Domain=de.artcom.gom-client-objc Code=404 "not found"
    
    • 销毁不存在的节点
    [gomClient destroy:@"/tests/node_x" completionBlock:^(NSDictionary *response, NSError *error) {
    
        // Your code here
    
    }];
    NSError Domain=de.artcom.gom-client-objc Code=404 "not found"
    

处理观察者

  • 注册观察者

    [gomClient registerGOMObserverForPath:@"/tests/node_1:attribute_2" clientCallback:^(NSDictionary *dict) {
    
        // Your code here
    
    }];

    立即收到第一个 GOM 通知

    {
        event_type = "initial"
        path = "/tests/node_1:attribute_2"
        payload = {
            attribute = {
                ctime = "2013-12-29T18:00:27+01:00";
                mtime = "2013-12-29T18:00:27+01:00";
                name = "attribute_2";
                node = "/tests/node_1";
                type = string;
                value = 20;
            }
        }
    }}
    
  • 注销观察者

    [gomClient unregisterGOMObserverForPath:@"/tests/node_1:attribute_2"];

将响应字典映射到数据对象

如果 GOM 的响应字典难以处理,可以使用以下类将字典映射到数据对象。

  • 接收属性时

    GOMAttribute *attribute = [GOMAttribute attributeFromDictionary:response];
  • 接收节点时

    GOMNode *node = [GOMNode nodeFromDictionary:response];

处理 WebSocket 重连

如果 Gom 客户端的 WebSocket 出现故障,它会向代理发送消息 - (BOOL)gomClientShouldReconnect:(GOMClient *)gomClient。返回 YES 以重新连接。您也可以通过调用 - (void)reconnectWebSocket 在稍后触发重新连接。

当 Gom 客户端重新连接并发现现有绑定时,它会向代理发送消息 - ( BOOL )gomClient:(GOMClient *)gomClient shouldReRegisterObserverWithBinding:(GOMBinding *)binding

返回 YES 以重新注册特定路径的观察者。重新注册将是静默的,不会收到初始 GNP。返回 NO 以丢弃现有绑定。

如果未实现此方法,则所有绑定都将被丢弃。

为客户端开发设置

要将项目设置为 GOM 客户端开发,请打开终端并克隆仓库

$ git clone https://github.com/artcom/gom-client-objc.git

然后使用 CocoaPods 依赖管理器安装所有必要的依赖项

$ cd demo-projects/gom-client-demo_iOS
$ pod install

您可以使用此项目中包含的示例应用程序来运行和测试您的工作。

所有依赖项都在 Podfile 文件中定义

示例应用程序

设置 GOM 根地址

Setting the GOM root

启动 - 示例应用程序提供了 GOM 节点或属性及其值的输入字段。下面的四个按钮代表您可以发送给 GOM 的命令

  • 检索
  • 创建
  • 更新
  • Del(ete)

来自 GOM 的所有响应和 GNP 将显示在上面的输出字段中

Startup

获取 GOM 值 - 只需输入节点或属性的路径,然后轻按 '检索'

Accessing GOM values

来自 GOM 的响应将显示在上面的输出字段中

Retrieving GOM values

添加观察者 - 轻按 '管理观察者' 打开观察者管理视图。输入节点或属性的路径,然后轻按 '添加观察者'

Adding a GOM observer

观察者列表 - 已注册的观察者将显示在上面的表格中。同一路径上的每个额外观察者都只会增加句柄数,显示为 '句柄' 项目

GOM observer added

删除观察者 - 拖动到左侧,出现 '删除' 按钮

Deleting a GOM observer