2013-10-08 2 views
1

jabber 지원을 위해 저는 Smack 라이브러리를 사용합니다. 안드로이드 포트 asmack.Phonegap jabber plugin for android

나는 MessageListener 인터페이스를 구현하고 메시지를 연결, 로그인, 보내기위한 메소드를 포함하고있는 SmackAPI 클래스를 가지고있다. 동시에이 클래스에는 메서드가 포함되어 있습니다.

@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; 
} 

MessageListener 인터페이스를 통해 제공됩니다. 이 메소드로 처리 된 모든 새 메시지

jabber 플러그인을 작성하여 연결, 로그인하고 phonegap에서 메시지를 보냅니다.

내 질문 : 어떻게 자바 스크립트에서 새 메시지를들을 수 있습니까?

답변

1

내가 해냈어. 그러나 나는 그것이 옳은 길인 것을 알고있다. 그러나 그것은 일한다!

코르도바 플러그인 클래스 :

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; 

그리고 쉽게 자바 스크립트. 그냥 플러그인 메서드를 호출 :

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

설명 : 내가 "listenMessage"(행동 "LISTEN_MESSAGE"과 "실행"메소드 호출) 플러그인 메서드를 호출합니다. 저기, 내가 runnable와 cordova threadpool에서 스레드를 시작 runnable에서 나는 메시지를 확인하는 재귀 함수가있어. 하지만 runnable 시작하기 전에 메서드를 호출하는 메서드의 callbackId를 가져와야합니다. 또한, 메서드에서 종료, 나는 "NO_RESULT"상태로 새로운 PluginResult를 생성하고 "keepCallback"옵션을 true로 설정합니다. 즉, 그 메소드는 javascript에서 하나 이상의 콜백 결과를 기다리고 있습니다. 메시지를 받았을 때 callbackid와 webview를 기반으로 새로운 callbackcontext를 만들고, setUpCallback을 true로 설정하고 pluginresult에 대한 가능한 응답을 추가하고 pluginresult에 "OK"상태의 메시지를 넣은 다음 callbackcontext로 보냅니다. 그게 다야.