javascript - XMPP:AngularJs + Strophe.js

标签 javascript angularjs xmpp strophe

我有一个使用 strophe.js 的基本 XMPP 客户端。

在登录时我创建处理程序,例如

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。第一部分非常简单。我有一个 Controller 可以很好地处理登录部分:

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");
  }
}
})

但是我如何真正开始在我拥有的所有 Controller 的 Angular 上下文中收听这些事件(on_message、on_presence)。

最佳答案

将 Strophe 包裹在 Angular Service 中. Angular 服务旨在用作单例,因此您将能够实例化 Strophe 服务一次,然后在任何地方使用它(使用依赖注入(inject))。

关于javascript - XMPP:AngularJs + Strophe.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24337123/

相关文章:

javascript - 嵌入式Youtube视频在Firefox上不起作用

ajax - AngularJS : single page and multipage app difference and use in practice?

javascript - 停止 ng repeat 对选项卡 ui 选择事件进行递归调用

java - 如何使用 Smack 将 XMPP 消息从 Android 发送到 GAE

XMPP strophe,获取存在名单

Javascript 类 - 如何使属性成为必需和可选

javascript - 从 JavaScript 对象创建嵌套无序列表(可以是 n 层深)

javascript - 禁用表单提交,直到使用 jQuery 验证字段

angularjs - 如何使用 Firebase 和 AngularJS 创建评论线程?

xmpp - 使用 Smack 4.1.0 的 GCM XMPP 服务器