gpt4 book ai didi

javascript - AngularJs + Strope.js 接收消息

转载 作者:行者123 更新时间:2023-11-28 00:37:06 25 4
gpt4 key购买 nike

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

我编写 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>

最佳答案

更新:我尝试将 on_presenceon_message 函数放入 Controller 内,并且它有效!

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

关于javascript - AngularJs + Strope.js 接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28375601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com