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

Nativescript, how to use this java eventListener in Javascript?

我正在使用 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));
    }
};

下面是 Java 将 SubscriptionEventListener 绑定到频道的代码:

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

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

的通道

谢谢

更新

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


SubscriptionEventListener 是一个接口,您应该实现方法并将实例传递给绑定方法,如 docs.

中所示
channel.bind("my-event", 
   new SubscriptionEventListener({
    onEvent: function(event) {
        console.log("Received event with data: " + event.toString());
    }
   })
);

几件事:

  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());
            }
          } );

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

感谢Nathanael 这是最终代码:

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;
        PusherEvent = com.pusher.client.channel.PusherEvent;
        SubscriptionEventListener = com.pusher.client.channel.SubscriptionEventListener;
        ChannelEventListener = com.pusher.client.channel.ChannelEventListener;

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

        pusher.connect();

        const channel = new Channel(pusher.subscribe(channel_name));
        const connectedChannel = pusher.getChannel(channel_name);

        let sel = new SubscriptionEventListener({
            onEvent: function(event) {
                console.log(event);
            }
        });

        connectedChannel.bind(event_name, sel);
    }
};