AngularJs + Strophe.js 接收消息

Posted

技术标签:

【中文标题】AngularJs + Strophe.js 接收消息【英文标题】:AngularJs + Strophe.js receive message 【发布时间】:2015-02-06 22:04:59 【问题描述】:

我有基于this link 的带有AngularJS 的基本XMPP 客户端。 user@user.local 发送消息成功,但是another@user.local 发送消息时user@user.local 仍然无法接收消息console.log("message")

我如何编写 addHandleronMessage 函数是否正确?

<html>
<head>
    <script type="text/javascript" src="bower_components/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/http-bind';
    xmpp_user = "user";
    xmpp_domain = "user.local";
    xmpp_userdomain = "user@user.local";
    xmpp_password = "userpassword";

    angular.
    module('myApp', []).
    controller('init', function(xmppAuth)
        xmppAuth.auth(xmpp_userdomain,xmpp_password);
    ).
    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("auth pass");

                        //try send helo
                        var message = "helo";
                        var to = "another@user.local";
                        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('message');
                            return true;
                        

                   
               )
            
        
    )

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

【问题讨论】:

【参考方案1】:

更新:我尝试将 on_presenceon_message 函数放在控制器中,它可以工作!

<html>
<head>
    <script type="text/javascript" src="bower_components/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/http-bind';
    xmpp_user = "user";
    xmpp_domain = "user.local";
    xmpp_userdomain = "user@user.local";
    xmpp_password = "userpassword";

    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("auth pass");

                        //try send helo
                        var message = "helo";
                        var to = "another@user.local";
                        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('message');
                            return true;
                        

                   
               )
            
        
    )

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

【讨论】:

【参考方案2】:

根据您提到的链接,我猜在 auth succees 上您应该将在线状态返回为 true。

var on_presence = function(presence) 
    // do some stuff
    return true;
;

根据 XMPP 协议,我必须在收到任何消息之前在线呈现。

【讨论】:

以上是关于AngularJs + Strophe.js 接收消息的主要内容,如果未能解决你的问题,请参考以下文章

strophe js 使用 angular 4 接收一条消息

strophe js MUC房间

使用 strophe js 在聊天中发送图像

旁遮普邦并不始终承认它正在从 strophe 获取消息

strophe.min.js 和 strophe.js 的区别?

strophe.js 聊天状态通知