【发布时间】:2019-10-01 14:20:13
【问题描述】:
我正在使用 NativeScript 并已将 Pusher-Java 库实现为依赖项, 我可以成功连接并订阅我的 Pusher 频道, 但我很难将 SubscriptionEventListener 添加到我的频道,
这是我在 Nativescript 中使用 java 库连接到推送器的代码:
module.exports = {
connect:function(app_key, channel_name, event_name) {
PusherOptions = com.pusher.client.PusherOptions;
Pusher = com.pusher.client.Pusher;
Channel = com.pusher.client.channel.Channel;
SubscriptionEventListener = com.pusher.client.channel.SubscriptionEventListener;
PusherEvent = com.pusher.client.channel.PusherEvent;
var options = new PusherOptions().setCluster("eu");
var pusher = new Pusher(app_key, options);
pusher.connect();
var channel = new Channel(pusher.subscribe(channel_name));
}
};
下面是绑定 SubscriptionEventListener 到频道的 Java 代码:
channel.bind("my-event", new SubscriptionEventListener() {
@Override
public void onEvent(PusherEvent event) {
System.out.println("Received event with data: " + event.toString());
}
});
现在如何使用 Javascript 绑定它!? 我已经尝试了任何我能想到的方法,但仍然无法使用 Javascript 将 SubscriptionEventListener 绑定到频道,
谢谢
更新
我正在使用这种方法,预计会起作用,@Manoj 也在这里回答了:
channel.bind(event_name,
new SubscriptionEventListener({
onEvent: function(event) {
console.log(event.toString());
}
})
);
但它不起作用,我收到此错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.plugintestproject/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: Error: Building UI from XML. @app-root.xml:1:1
System.err: > java.lang.AbstractMethodError: abstract method "void com.pusher.client.channel.Channel.bind(java.lang.String, com.pusher.client.channel.SubscriptionEventListener)"
System.err: com.tns.Runtime.callJSMethodNative(Native Method)
【问题讨论】:
标签: javascript java nativescript pusher nativescript-plugin