Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJs+;Strophe.js接收消息_Javascript_Angularjs_Xmpp_Strophe - Fatal编程技术网

Javascript AngularJs+;Strophe.js接收消息

Javascript AngularJs+;Strophe.js接收消息,javascript,angularjs,xmpp,strophe,Javascript,Angularjs,Xmpp,Strophe,我有基于AngularJS的基本XMPP客户端user@user.local发送消息时成功,但user@user.local当another@user.local发送一条消息 我编写addHandler和onMessage函数的方式正确吗 <html> <head> <script type="text/javascript" src="bower_components/angular/angular.min.js"></script>

我有基于AngularJS的基本XMPP客户端<代码>user@user.local发送消息时成功,但
user@user.local
another@user.local
发送一条消息

我编写
addHandler
onMessage
函数的方式正确吗

<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>

波什乌服务中心http://localhost/http-bind';
xmpp_user=“user”;
xmpp_domain=“user.local”;
xmpp_userdomain=”user@user.local";
xmpp_password=“userpassword”;
有棱角的
模块('myApp',[])。
控制器('init',函数(xmppAuth){
auth(xmpp_用户域,xmpp_密码);
}).
服务('xmppAuth',函数(){
返回{
auth:函数(登录名、密码){
connect=新的选通连接(BOSH_服务);
connect.connect(登录、密码、功能(状态){
如果(状态===Strophe.status.CONNECTED){
console.log(“身份验证过程”);
//试着送他去
var message=“helo”;
var to=”another@user.local";
如果(消息(&to){
var reply=$msg({
致:致,,
键入:“聊天”
})
.cnode(Strophe.xmlement('body',message)).up()
.c('active',{xmlns:'http://jabber.org/protocol/chatstates"});
连接。发送(回复);
log('我发送了'+到+':''+消息);
}
//addhandler接收消息
addHandler(onMessage,null,“message”,null,null,null);
var onMessage=函数(消息){
console.log('message');
返回true;
}
}
})
}
}
})

根据您提到的链接,我想在认证成功后,您应该将在线状态作为true返回

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

根据XMPP协议,在接收任何消息之前,我的be接收器都应该在线显示。

更新:我尝试在控制器内设置
显示状态
显示消息
功能,它可以工作

<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>

波什乌服务中心http://localhost/http-bind';
xmpp_user=“user”;
xmpp_domain=“user.local”;
xmpp_userdomain=”user@user.local";
xmpp_password=“userpassword”;
有棱角的
模块('myApp',[])。
控制器('init',函数(xmppAuth){
auth(xmpp_用户域,xmpp_密码);
on_存在=功能(存在){
console.log('presence');
返回true;
}
on_消息=功能(消息){
//console.log('message');
控制台日志(消息);
返回true;
}
}).
服务('xmppAuth',函数(){
返回{
auth:函数(登录名、密码){
connect=新的选通连接(BOSH_服务);
connect.connect(登录、密码、功能(状态){
如果(状态===Strophe.status.CONNECTED){
console.log(“身份验证过程”);
//试着送他去
var message=“helo”;
var to=”another@user.local";
如果(消息(&to){
var reply=$msg({
致:致,,
键入:“聊天”
})
.cnode(Strophe.xmlement('body',message)).up()
.c('active',{xmlns:'http://jabber.org/protocol/chatstates"});
连接。发送(回复);
log('我发送了'+到+':''+消息);
}
//addhandler接收消息
addHandler(onMessage,null,“message”,null,null,null);
var onMessage=函数(消息){
console.log('message');
返回true;
}
}
})
}
}
})