亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Nativescript,如何在Javascript中使用這個java eventListener?

Nativescript,如何在Javascript中使用這個java eventListener?

滄海一幻覺 2023-09-20 14:30:39
我正在使用 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 failedSystem.err: Error: Building UI from XML. @app-root.xml:1:1System.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)
查看完整描述

3 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

有幾件事:

  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傳遞給它的參數。


查看完整回答
反對 回復 2023-09-20
?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

SubscriptionEventListener是一個接口,您應該實現方法并將實例傳遞給綁定方法,如文檔中所示。


channel.bind("my-event",?

? ?new SubscriptionEventListener({

? ? onEvent: function(event) {

? ? ? ? console.log("Received event with data: " + event.toString());

? ? }

? ?})

);


查看完整回答
反對 回復 2023-09-20
?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

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

    }

};


查看完整回答
反對 回復 2023-09-20
  • 3 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號