【问题标题】:Nativescript, how to use this java eventListener in Javascript?Nativescript,如何在Javascript中使用这个java eventListener?
【发布时间】:2019-10-01 14:20:13
【问题描述】:

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


【问题讨论】:

    标签: javascript java nativescript pusher nativescript-plugin


    【解决方案1】:

    几件事:

    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 提供的详细信息,我想让它尽可能原生,现在如何将它绑定到频道?我在 console.log() 什么也没得到;并尝试 channel.bind() 但出错
    • 当我尝试将 eventListener 绑定到通道时出现抽象方法错误:System.err: > java.lang.AbstractMethodError: abstract method "void com.pusher.client.channel.Channel.bind(java .lang.String, com.pusher.client.channel.SubscriptionEventListener)"
    • 如果你越来越抽象;你确定你有一个有效的频道吗?听起来您已经直接从通道对象创建了一个新的空通道,并且没有通过推送器接口正确创建通道,从而为您提供了正确构建的通道对象。如果你这样做会发生什么:const channel = connectedPusher.getChannel(channelName); 你得到一个有效的频道吗?或空/未定义。
    【解决方案2】:

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

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

    【讨论】:

    • 我已经试过了,现在又试了一次,但是我得到了这个错误:``` Caused by: com.tns.NativeScriptException: Calling js method onCreate failed System.err: Error: Building来自 XML 的用户界面。 @app-root.xml:1:1 System.err: > java.lang.AbstractMethodError: 抽象方法 "void com.pusher.client.channel.Channel.bind(java.lang.String, com.pusher.client.channel .SubscriptionEventListener)" System.err: com.tns.Runtime.callJSMethodNative(Native Method) ```
    • 我不熟悉这个库。但不是new Channel(pusher.subscribe(channel_name));,repo 中的示例使用pusher.subscribe("my-channel", new ChannelEventListener({...})。你试过了吗?
    • 我现在试过了,得到这个:java.lang.Exception: Failed resolve method subscribe on class com.pusher.client.Pusher
    • 我不明白为什么,因为您已经在代码中使用了 subscribe 方法。您是否尝试过生成类型?
    【解决方案3】:

    感谢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);
        }
    };
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 2020-12-11
      • 1970-01-01
      相关资源
      最近更新 更多