AngularJs + Strophe.js receive message

I have basic XMPP client with AngularJS based on this link. [email protected] succeeded when sending a message but the [email protected] still cannot receive the message console.log("message") when [email protected] sends a message.

Is it correct on how I write the addHandler and onMessage functions?

<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 = "[email protected]";
    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 = "[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('message');
                            return true;
                        }

                   }
               })
            }
        }
    })

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

Answers:

Answer

UPDATE : I try to put on_presence and on_message functions inside the controller, and it works !

<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 = "[email protected]";
    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 = "[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('message');
                            return true;
                        }

                   }
               })
            }
        }
    })

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

As per link you have mentioned, I guess on auth succees you should return presence online as true.

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

As per XMPP protocol my be before receiving any message receiver should present online.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.