2015-08-27 63 views
3

我有這個code.I發送消息到服務器,但我沒有收到他們。我有一個帖子的用戶說,工作的代碼。我有一個xmpp服務器,我可以連接strophe。爲什麼我不能在strophe js中收到消息?

<html> 
<head> 
    <script type="text/javascript" src="angular/angular.min.js"></script> 
    <script type="text/javascript" src="strophe.min.js"></script> 
</head> 
<body ng-app="myApp"> 
    <div ng-controller="init"> 
    </div> 

    <script type="text/javascript"> 

    BOSH_SERVICE = 'http://localhost:5280/http-bind/'; 
    xmpp_user = "user"; 
    xmpp_domain = "localhost"; 
    xmpp_userdomain = "[email protected]"; 
    xmpp_password = "secret"; 

    angular. 
    module('myApp', []). 
    controller('init', function(xmppAuth){ 
     xmppAuth.auth(xmpp_userdomain,xmpp_password); 

     on_presence = function (presence){ 
      console.log('presence'); 
      return true; 
     } 

     on_message = function (message){ 
      //console.log('message'); 
      console.log(message); 
      return true; 
     } 
    }). 
    service('xmppAuth', function() { 
     return { 
      auth: function(login, password) { 
       connect = new Strophe.Connection(BOSH_SERVICE); 
       connect.connect(login, password, function (status) { 
        if (status === Strophe.Status.CONNECTED) { 
         console.log("Autentificare reusita!"); 

         //try send helo 
         var message = "helo"; 
         var to = "[email protected]"; 
         if(message && to){ 
          var reply = $msg({ 
           to: to, 
           type: 'chat' 
          }) 
          .cnode(Strophe.xmlElement('body', message)).up() 
          .c('active', {xmlns: "http://jabber.org/protocol/chatstates"}); 
          connect.send(reply); 
          console.log('I sent ' + to + ': ' + message); 
         } 

         //addhandler receive messg 
         connect.addHandler(onMessage, null, "message", null, null, null); 
         var onMessage = function (message){ 
          console.log('S-a primit un mesaj'); 
          console.log('message'); 
          return true; 
         } 

        } 
       }) 
      } 
     } 
    }) 

    </script> 
</body> 
</html> 

我該怎麼辦?感謝您的幫助!

回答

0

您不會收到您發送的消息,只接收傳入的消息或使用MUC插件獲取歷史記錄。

2

我有一個類似的問題,我發現在發送任何消息之前設置處理程序將允許您閱讀消息,甚至是您發送的消息。 下面是我測試

一個工作代碼...

server_connection.connect(user_id, password, function(status){ 
    if (status == Strophe.Status.CONNECTED){ 
      on_connected(); 
    } 
}); 



function on_connected(){ server_connection.addHandler(on_message, null, 'message');} 

var on_message = function(message){ /* work with message here */ } 
相關問題