我正在使用 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)
桃花长相依
万千封印
慕村9548890
相关分类