【问题标题】:Not able to receive XMPPPubsub events to node subscribers无法接收到节点订阅者的 XMPPubsub 事件
【发布时间】:2015-11-13 16:23:39
【问题描述】:

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

在 XMPPubsub 中,所有者创建一个节点,其他想要获取事件通知的用户订阅该节点。

  1. 设置 XMPPubsub

    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"}];
    }
    

一切正常 用户可以订阅节点。我们能够创建节点。当我们获取订阅者节点时,它也会返回所有节点。我们发布的事件也在数据库中,如果我通过代码检索事件,它也会返回所有事件。

但问题是订阅者无法在 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;
}

请指导我完成我在其中做错了什么。

提前致谢。

【问题讨论】:

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


【解决方案1】:

客户应表明他希望接收通知,如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"];
}

【讨论】:

  • 只有一个节点会变成静态的。但我想收到我订阅的所有点头的通知。
  • 这样您就可以查询订阅节点列表并为每个节点发布+通知。也许你可以配置一些 pubsub 服务器组件来默认启用通知,但不建议这样做。
  • 在 xmppModule 类中,如果我们使用 XMPPCapabilities 它会给出前向类声明错误。因为 XMPPModule 是 XMPPCapabilities 的超类
  • 你需要在你的 XMPPFramework.h 中导入 XMPPCapabilites.h
  • 我确实已经在那儿了。我已经有了 _xmppCapabilities = [[XMPPCapabilities alloc] initWithCapabilitiesStorage:_xmppCapabilitiesStorage];在设置流中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多