简体   繁体   English

适用于Android的Phonegap Jabber插件

[英]Phonegap jabber plugin for android

For jabber support i use library Smack. 对于jabber支持,我使用库Smack。 Android port asmack. Android端口asmack。

I have class SmackAPI which implements MessageListener interface and contains methods to connect, login, send message. 我有SmackAPI类,该类实现MessageListener接口,并包含用于连接,登录和发送消息的方法。 In the same time this class contains method: 同时,此类包含方法:

@Override
public void processMessage(Chat chat, Message message) {
    String from = message.getFrom();
    String body = message.getBody();
    System.out.println(String.format("Received message '%1$s' from %2$s", body, from));
    this.recievedMessage = message;
}

It provides by MessageListener interface. 它通过MessageListener接口提供。 All new messages processed by this method. 此方法处理的所有新邮件。

I write jabber plugin to connect, login, send message from phonegap. 我写了jabber插件来连接,登录并从phonegap发送消息。

My question: how i can in javascript listen for new messages? 我的问题:如何在javascript中收听新消息?

I did it. 我做的。 I dont know however it is right way, but it works! 我不知道这是正确的方法,但是有效!

Cordova plugin class: Cordova插件类:

public class SmackJabber extends CordovaPlugin {
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    this.cbContext = callbackContext;
    switch (action) {
            case LISTEN_MESSAGE:
                res = new PluginResult(PluginResult.Status.NO_RESULT);
                res.setKeepCallback(true);
                cordova.getThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        String callbackId = cbContext.getCallbackId();
                        while (true) {
                            String msg = getMsg();
                            if (msg != null) {
                                res = new PluginResult(PluginResult.Status.OK, msg);
                                res.setKeepCallback(true);
                                CallbackContext cb = new CallbackContext(callbackId, webView);
                                cb.sendPluginResult(res);
                            }
                        }
                    }
                });
                cbContext.sendPluginResult(res);
                break; 

And easy javascript. 和简单的JavaScript。 Just call plugin method: 只需调用插件方法:

        window.plugins.smackJabber.listenMessage(function(result) {
                    alert(result)
                }, function(error) {
                    alert(error)
                }
        );

Explanation: I call plugin method "listenMessage" (calling "execute" method with action "LISTEN_MESSAGE"). 说明:我将插件方法称为“ listenMessage”(通过动作“ LISTEN_MESSAGE”调用“ execute”方法)。 There i start thread from cordova threadpool with runnable, in runnable i got recursive function which check message. 在那里,我从可运行的Cordova线程池启动线程,在可运行的线程中,我得到了检查消息的递归函数。 But before start runnable i have to take callbackId of method who call method execute. 但是在开始运行之前,我必须采取调用方法execute的方法的callbackId。 Also, for exit from method, i create new PluginResult with status "NO_RESULT" and set it option "keepCallback" to true - it means, that method calls in javascript awaiting one more callback result from me. 另外,要从方法中退出,我将创建状态为“ NO_RESULT”的新PluginResult并将其选项“ keepCallback”设置为true-这意味着该方法将在javascript中调用,以等待我的另一个回调结果。 When i got message, i create new callbackcontext based on callbackid and my webview, do setKeepCallback to true for futher possible responses for pluginresult, putting in pluginresult my message with status "OK" and sending it to callbackcontext. 收到消息后,我将基于callbackid和我的webview创建新的callbackcontext,将setKeepCallback设置为true以获得对pluginresult的进一步可能的响应,将pluginresult的状态为“ OK”的消息发送到callbackcontext。 That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM