无法向节点订阅者

时间:2015-08-20 06:45:03

标签: ios objective-c xmpp publish-subscribe xmppframework

我正在开发像RSS Feed这样的应用程序。我希望用户在不调用任何Web服务的情况下获取提要。我读了一些文档并得到了使用XMPP的解决方案,其中XMPPPubsub为我们提供了向所有订阅者发送事件通知的功能。

在XMPPPubsub中,所有者创建一个节点,并希望获得订阅该节点的事件通知的其他用户。

  1. 设置XMPPPubsub

    XMPPJID *serviceJID =[XMPPJID jidWithString:[NSString stringWithFormat:@"pubsub.%@",HOST_NAME]];
    _xmppPubSub = [[XMPPPubSub alloc]initWithServiceJID:serviceJID     dispatchQueue:dispatch_get_main_queue()];
    [_xmppPubSub addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [_xmppPubSub activate:_xmppStream];
    
  2. 创建节点

    - (void)createNodeWithName:(NSString*)nodeName withCompletionHandler:(CreateNodeCompletionHandler)completionBlock{
    
       _createNodeCompletionHandler = completionBlock;
    
       [_xmppPubSub createNode:nodeName withOptions:@{@"pubsub#title":nodeName,@"pubsub#deliver_notifications":@"1",@"pubsub#deliver_payloads":@"0",@"pubsub#persist_items":@"1",@"pubsub#notify_sub":@"1",@"pubsub#subscribe":@"1",@"pubsub#send_last_published_item":@"When a new subscription is processed and whenever a subscriber comes online",@"pubsub#access_model":@"open",@"pubsub#presence_based_delivery":@"1",@"pubsub#publish_model":@"open"}];
    }
    
  3. 想要拥有活动的用户必须订阅节点

    - (void)subScribeToNode:(NSString*)nodeName withCompletionHandler:(SubscribeNodeCompletionHandler)completionBlock{
    
      _subscribeNodeCompletionHandler = completionBlock;
      [_xmppPubSub subscribeToNode:nodeName withJID:_xmppStream.myJID options: @{ @"pubsub#deliver"      : @(YES),
                                                                    @"pubsub#digest"       : @(YES),
                                                                     @"pubsub#include_body" : @(YES),
                                                                     @"pubsub#show-values"  : @[ @"chat", @"online", @"away" ] }];
     }
    
  4. 将事件发布到节点

    - (void)sendMessage:(NSString*)message ToNode:(NSString*)node{
    NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
    [body setStringValue:message];
    
    NSXMLElement *messageBody = [NSXMLElement elementWithName:@"message"];
    [messageBody setXmlns:@"jabber:client"];
    [messageBody addChild:body];
    [_xmppPubSub publishToNode:node entry:messageBody withItemID:nil options:@{@"pubsub#access_model":@"open"}];
    }
    
  5. 一切运行良好用户可以订阅节点。我们能够创建节点。当我们获得订户节点时,它也返回所有节点。我们发布的事件也存在于数据库中,如果我按代码检索事件,它也会返回所有事件。

    但问题是订阅者无法在 didReceiveMessage 中收到通知,因此订阅者无法收到有关事件的通知。

    以下功能

    中没有日志
        - (void)xmppPubSub:(XMPPPubSub *)sender didReceiveMessage:(XMPPMessage *)message{
      NSLog(@" -----We Just Received message wooo hooo whow owowhwo -----");
    }
        - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{
        return YES;
    }
    

    请引导我完成我在做错的事情。

    先谢谢。

1 个答案:

答案 0 :(得分:0)

客户应表示希望收到通知,如XEP-0060

中所述

在XMPPFramework中,您需要实现XMPPCapabilitiesDelegate并将younodename+notify添加到客户端功能。例如。在XMPPModule中,您需要以下内容:

- (BOOL)activate:(XMPPStream *)aXmppStream
{
    if ([super activate:aXmppStream])
    {
        [xmppStream autoAddDelegate:self delegateQueue:moduleQueue toModulesOfClass:[XMPPCapabilities class]];
        return YES;
    }

    return NO;
}
- (NSArray *)myFeaturesForXMPPCapabilities:(XMPPCapabilities *)sender
{
    return @["myprotocolnode+notify"];
}