gpt4 book ai didi

javascript - Nativescript,如何在Javascript中使用这个java eventListener?

转载 作者:行者123 更新时间:2023-12-02 09:28:11 29 4
gpt4 key购买 nike

我正在使用 NativeScript 并已实现 Pusher-Java Library 作为依赖项,我可以成功连接并订阅我的 Pusher channel ,但我在向我的 channel 添加 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 绑定(bind)到 channel 的 Java 代码:

channel.bind("my-event", new SubscriptionEventListener() {
@Override
public void onEvent(PusherEvent event) {
System.out.println("Received event with data: " + event.toString());
}
});

现在我如何使用 Javascript 绑定(bind)它!?我已经尝试了所有我能想到的方法,但仍然无法使用 Javascript 将 SubscriptionEventListener 绑定(bind)到 channel ,

谢谢

更新

我正在使用这种方法,预计会起作用,@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)


最佳答案

有几件事:

  1. 为什么不直接使用 nativescript-pusher 插件呢?它已经存在了...

  2. 第二,如果你不想使用它;为什么不借用代码,因为它是在 Apache 2.0 许可证下的。

<小时/>

但是,要具体回答您的问题:

const sel = new com.pusher.client.channel.SubscriptionEventListener( {
onEvent: function(channel, event, data) {
console.log("Channel:", channel, "Event", event, "received event with data: " + data.toString());
}
} );

首先,在创建事件时您确实应该使用完整的命名空间(这使得正在创建的内容一目了然)。
其次,您的 onEvent 原型(prototype)是错误的。根据文档,它是 Channel、Event、Data 是传递给它的参数。

关于javascript - Nativescript,如何在Javascript中使用这个java eventListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58186840/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com