编写NativeScript插件的正确语法

编写NativeScript插件的正确语法,nativescript,angular2-nativescript,nativescript-plugin,Nativescript,Angular2 Nativescript,Nativescript Plugin,我正在学习NativeScript插件,并试图让PubNub iOS SDK正常工作。到目前为止(使用下面的TypeScript),我能够成功地配置、订阅频道和发布消息。我还试图通过将“//handlenewmessage…”部分转换为TypeScript来接收消息,但未能使其正常工作。我该怎么写呢 目标C: // Initialize and configure PubNub client instance PNConfiguration *configuration = [PNConfigu

我正在学习NativeScript插件,并试图让PubNub iOS SDK正常工作。到目前为止(使用下面的TypeScript),我能够成功地配置、订阅频道和发布消息。我还试图通过将“//handlenewmessage…”部分转换为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)
})

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

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


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

啊,我明白了,这个示例很清楚,我能够使用PNObjectEventListener协议创建一个委托,并像您所说的那样将该实例传递给addListener,然后使用clientDidReceiveMessage回调成功获取并将消息传递回所有者