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

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


沧海一幻觉
浏览 114回答 3
3回答

桃花长相依

有几件事:为什么不直接使用 nativescript-pusher 插件呢?它已经存在了...第二,如果你不想使用它;为什么不借用代码,因为它是在 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传递给它的参数。

万千封印

SubscriptionEventListener是一个接口,您应该实现方法并将实例传递给绑定方法,如文档中所示。channel.bind("my-event",    new SubscriptionEventListener({    onEvent: function(event) {        console.log("Received event with data: " + event.toString());    }   }));

慕村9548890

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);    }};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java