gpt4 book ai didi

javascript - 如何在 NativeScript 中从 Java 获取定义的实例/变量?

转载 作者:行者123 更新时间:2023-11-30 05:01:33 27 4
gpt4 key购买 nike

我已经创建了一个插件,它使用这个 Java 插件通过 NativeScript 成功地连接和订阅了推送器 channel ,现在我正在尝试创建一个 eventListener 以获取 Nativescript 中的事件,

这是我的Java 插件:

public class PusherAndroid {

public void connectToPusher(String app_key, String channel_name, String event_name) {

PusherOptions options = new PusherOptions().setCluster("eu");
Pusher pusher = new Pusher(app_key, options);

pusher.connect(new ConnectionEventListener() {
@Override
public void onConnectionStateChange(ConnectionStateChange change) {
System.out.println("State changed to " + change.getCurrentState() +
" from " + change.getPreviousState());
}

@Override
public void onError(String message, String code, Exception e) {
System.out.println("There was a problem connecting!");
}
}, ConnectionState.ALL);

Channel channel = pusher.subscribe(channel_name);

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

}
}

这是我的模块:

module.exports = {
connect:function(app_key, channel_name, event_name) {
var psh = new com.pxb.pusherandroid.PusherAndroid();
psh.connectToPusher(app_key, channel_name, event_name);

var EventListener;
function initializeEventListener() {
if (EventListener) {
return;
}
EventListener = com.pxb.pusherandroid.PusherAndroid.extend({
interfaces: [com.pusher.client.channel.SubscriptionEventListener],
onEvent: event => {
console.log(event);
}
});
}

initializeEventListener();
<HERE I NEED MY CHANNEL>.bind(event_name, new EventListener());
}
};

现在,我如何在 Javascript 中获取此 channel ,将其用作我定义的已连接 channel 并将 eventListener 绑定(bind)到它?

Channel channel = pusher.subscribe(channel_name);

谢谢

最佳答案

我真的不知道 NativeScript 是如何工作的,但你不能将你的 Channel 序列化为一个 json 字符串,将它存储在你的 PusherAndroid 类的全局变量中,然后然后在您的模块上访问并反序列化它?

关于javascript - 如何在 NativeScript 中从 Java 获取定义的实例/变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58165796/

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