XMPP:AngularJs + Strophe.js [英] XMPP: AngularJs + Strophe.js
本文介绍了XMPP:AngularJs + Strophe.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个基本的XMPP客户端工作strophe.js。
I have a basic XMPP client working on strophe.js.
在登录创建处理程序,如
On login I create handlers such as
connect = new Strophe.Connection('http://localhost/http-bind');
...
...
connect.addHandler(on_message, null, "message", "chat");
connect.addHandler(on_presence, null, "presence");
...
...
然后我听那些
function on_presence(presence) {
// handling presence
}
function on_message(presence) {
// handling presence
}
所以我想转换入AngularJS。第一部分是pretty直线前进。我有一个控制器,它处理登录部分就好了:
So I am trying to "convert" it into AngularJS. The first part is pretty straight forward. I have a controller which handles the login part just fine:
angular.module('app').controller('loginCtrl', function($scope) {
connect = new Strophe.Connection('http://website.com/http-bind');
connect.connect(data.jid, data.password, function (status) {
if (status === Strophe.Status.CONNECTED) {
connect.addHandler(on_message, null, "message", "chat");
connect.addHandler(on_presence, null, "presence");
}
}
})
但我怎么真正开始听着角的情况下这些事件(ON_MESSAGE,ON_ presence)对所有的控制器我有。
But how do I actually begin listening to those events (on_message, on_presence) in the context of angular across all the controllers I have.
推荐答案
正如上文所述(或波纹管)我包的strophe的服务,让我登录机制是这样的:
As suggested above (or bellow) I wrapped strophe in a service so my login "mechanism" looks like this:
.controller('loginCtrl', function(xmppAuth) {
xmppAuth.auth(login, password);
})
我的任何服务:
.service('xmppAuth', function() {
return {
auth: function(login, password) {
connect = new Strophe.Connection('http://mydomain.net/http-bind');
connect.connect(login, password, function (status) {
if (status === Strophe.Status.CONNECTED) {
// we are in, addHandlers and stuff
}
}
}
}
})
这篇关于XMPP:AngularJs + Strophe.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文