用于编写NativeScript插件的正确语法

时间:2018-11-09 10:17:26

标签: nativescript angular2-nativescript nativescript-plugin

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

Objective-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)
})

1 个答案:

答案 0 :(得分:1)

好像您在这里缺少Callable[[Arg1Type, Arg2Type], ReturnType] 委托。您应该实现委托并将其实例传递给PNObjectEventListener函数,以便在收到新消息时调用addListener回调。

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

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