XMPP: AngularJS + Strophe

2019-08-30 06:52发布

The basic XMPP with strophe and javascript wants to convert to AngularJS.

.controller('loginCtrl', function(xmppAuth) {

    xmppAuth.auth(login, password);

})

and in service:

    .service('xmppAuth', function() {

.return {

    auth: function(login, password) {
       connect = new Strophe.Connection(domain);
       connect.connect(login, password, function (status) {
           if (status === Strophe.Status.CONNECTED) {
               connect.addHandler(on_roster_changed,"jabber:iq:roster", "iq", "set");
               connect.addHandler(on_iq, null, "iq","");
               connect.addHandler(on_presence, null, "presence");
               connect.addHandler(on_message, null, 'message', '');
           }
       }
    }

    }

})

in js file

var on_presence = function(presence){
    code
}

when i run this there is no error. But all handling events like on_presence() method called only once. this is handler event of Strophe Connection object. Is there is any remain in this code or what should i do for handling strophes event with angularJS?

I refered This Link but it not works.

1条回答
叛逆
2楼-- · 2019-08-30 07:41

See the Strophe.js docs for addHandler:

The handler should return true if it is to be invoked again; returning false will remove the handler after it returns.

Therefore, your on_presence code should return true if you want it to be invoked again:

var on_presence = function(presence) {
    // do some stuff
    return true;
};
查看更多
登录 后发表回答