Javascript 为什么我能';无法在strophe js中接收消息?
我有这个代码。我向服务器发送消息,但我没有收到它们。我有来自用户说正在工作的帖子的代码。我有一个xmpp服务器,可以在那里连接stropheJavascript 为什么我能';无法在strophe js中接收消息?,javascript,strophe,Javascript,Strophe,我有这个代码。我向服务器发送消息,但我没有收到它们。我有来自用户说正在工作的帖子的代码。我有一个xmpp服务器,可以在那里连接strophe <html> <head> <script type="text/javascript" src="angular/angular.min.js"></script> <script type="text/javascript" src="strophe.min.js">&
<html>
<head>
<script type="text/javascript" src="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:5280/http-bind/';
xmpp_user = "user";
xmpp_domain = "localhost";
xmpp_userdomain = "user@localhost";
xmpp_password = "secret";
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("Autentificare reusita!");
//try send helo
var message = "helo";
var to = "marian@localhost";
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('S-a primit un mesaj');
console.log('message');
return true;
}
}
})
}
}
})
</script>
</body>
</html>
波什乌服务中心http://localhost:5280/http-绑定/';
xmpp_user=“user”;
xmpp_domain=“localhost”;
xmpp_userdomain=”user@localhost";
xmpp_password=“secret”;
有棱角的
模块('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){
log(“Autentificare reusita!”);
//试着送他去
var message=“helo”;
var to=”marian@localhost";
如果(消息(&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('S-a primit un mesaj');
console.log('message');
返回true;
}
}
})
}
}
})
我该怎么办?谢谢你的帮助 您将不会收到您发送的消息,只接收传入消息,或使用MUC插件记录历史。我遇到了类似的问题,我注意到在发送任何消息之前设置处理程序将允许您阅读消息,即使是您发送的消息。 下面是我已经测试过的工作代码
server_connection.connect(user_id, password, function(status){
if (status == Strophe.Status.CONNECTED){
on_connected();
}
});
function on_connected(){ server_connection.addHandler(on_message, null, 'message');}
var on_message = function(message){ /* work with message here */ }