【问题标题】:Correct syntax for writing NativeScript plugin编写 NativeScript 插件的正确语法
【发布时间】:2019-04-12 21:50:39
【问题描述】:

我正在学习 NativeScript 插件,并试图让 PubNub iOS SDK 正常工作。到目前为止(使用下面的 TypeScript),我能够成功配置、订阅频道和发布消息。我也尝试通过将“// 处理新消息...”部分也转换为 TypeScript 来接收消息,但无法使其正常工作。这个要怎么写?

目标-C:

// Initialize and configure PubNub client instance
PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"demo" subscribeKey:@"demo"];
self.client = [PubNub clientWithConfiguration:configuration];
[self.client addListener:self];

// Subscribe to demo channel with presence observation
[self.client subscribeToChannels: @[@"my_channel"] withPresence:YES];

// Handle new message from one of channels on which client has been subscribed.
- (void)client:(PubNub *)client didReceiveMessage:(PNMessageResult *)message {
    NSLog(@"Received message");
}

// Publish message
[self.client publish: @{@"message": @"this is my message"} 
           toChannel: @"my_channel" withCompletion:^(PNPublishStatus *status) {
}];

打字稿:

// Initialize and configure PubNub client instance
this.config = PNConfiguration.configurationWithPublishKeySubscribeKey("demo", "demo");
this.client = PubNub.clientWithConfiguration(this.config);
this.client.addListener();

// Subscribe to demo channel with presence observation
this.client.subscribeToChannelsWithPresence(channels, true);

// Handle new message from one of channels on which client has been subscribed. 
   ?

// Publish message
this.client.publishToChannelWithCompletion(msgObj, channel, function(publishStatus) {
  console.log(publishStatus.data)
})

【问题讨论】:

    标签: nativescript angular2-nativescript nativescript-plugin


    【解决方案1】:

    看起来您在这里缺少PNObjectEventListener 代表。您应该实现委托并将其实例传递给 addListener 函数,以便在收到新消息时调用 didReceiveMessage 回调。

    例如here,您可以看到核心框架如何为 TextView 实现 UITextViewDelegate,以便在更改和其他事件时通知它。

    由于您使用的是 TypeScript,请为您的 PubNub 库利用 typings,以便您轻松找到正确的语法。

    【讨论】:

    • 啊,我明白了,这个例子很清楚,我能够使用 PNObjectEventListener 协议创建一个委托,并将该实例传递给 addListener,就像你说的那样,然后使用 clientDidReceiveMessage 回调来获取并传递消息成功返回所有者
    猜你喜欢
    • 1970-01-01
    • 2019-04-06
    • 2013-12-08
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多