Javascript 如何从NativeScript中的Java获取已定义的实例/变量?

Javascript 如何从NativeScript中的Java获取已定义的实例/变量?,javascript,java,android,nativescript,pusher,Javascript,Java,Android,Nativescript,Pusher,我已经创建了一个插件,使用这个Java插件通过NativeScript成功地连接并订阅了pusher频道, 现在我正在尝试创建一个eventListener来获取Nativescript中的事件 这是我的Java插件: public class PusherAndroid { public void connectToPusher(String app_key, String channel_name, String event_name) { PusherOptio

我已经创建了一个插件,使用这个Java插件通过NativeScript成功地连接并订阅了pusher频道, 现在我正在尝试创建一个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());
    }
};

这是我的模块:

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


谢谢你

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

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

多亏了,没有必要用Java编写代码,也不需要在Javascript中尝试使用它们, 我们可以直接将Java类和方法与Javascript结合使用, 这方面的参考资料实在太少了

删除所有java代码后,直接从pusher 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));

    }
};
现在将添加我的带有Javascript的eventlistener,因为,不需要用Java编写代码,也不需要尝试在Javascript中使用它们, 我们可以直接将Java类和方法与Javascript结合使用, 这方面的参考资料实在太少了

删除所有java代码后,直接从pusher 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));

    }
};

现在要添加我的eventlistener和Javascript,即使是在Java中,也不能从方法外部访问方法内部的局部变量。你可能不得不重新考虑你的班级结构。我想知道你为什么不用JavaScript做所有的事情,这样你就可以在一个地方访问所有的东西。或者您可以使用所需的参数从Java调用OneEvent。@Manoj您能用一个例子解释一下吗?这方面的参考文献不多,而且我已经经历了这么长时间的麻烦,我如何改进结构,以便主要使用javascript来控制它?因为我“根本”不是一个Java开发人员,谢谢谢谢Hank you@Manoj,我不知道我可以直接从Javascript使用它,我用Javascript重新编写了所有内容,并删除了我的Java.aar文件,所有的工作都很完美!即使在Java中,也不能从方法外部访问方法内部的局部变量。你可能不得不重新考虑你的班级结构。我想知道你为什么不用JavaScript做所有的事情,这样你就可以在一个地方访问所有的东西。或者您可以使用所需的参数从Java调用OneEvent。@Manoj您能用一个例子解释一下吗?这方面的参考文献不多,而且我已经经历了这么长时间的麻烦,我如何改进结构,以便主要使用javascript来控制它?因为我“根本”不是一个Java开发人员,谢谢谢谢Hank you@Manoj,我不知道我可以直接从Javascript使用它,我用Javascript重新编写了所有内容,并删除了我的Java.aar文件,所有的工作都很完美!