我是phonegap的新手。我使用coredova最新版本2.9.0通过连接到开放式服务器的xmpp服务器开发聊天应用程序..我一直在搜索phonegap中的相关storph.js代码,自过去2天以来我没有在phonegap中获取正在运行的代码,获得状态“1”意味着连接......任何人都可以帮助...谢谢你的推荐。
<html>
<head>
<title>
phonegap xmpp tutorial
</title>
</head>
<script type="text/javascript" charset="utf-8" src="coredova-2.9.0.js"></script>
<script type="text/javascript" charset="utf-8" src="strophe.js"></script>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'></script>
<script>
function connect() {
var username="xxx";
var HOST_DOMAIN="xxx";
var password="xxx";
var BOSH_SERVICE = "xxxxx";
connection = new Strophe.Connection(BOSH_SERVICE);
connection.connect(username + "@" + HOST_DOMAIN, password, onConnect);
}
function onConnect(status) {
alert(status);
if (status == Strophe.Status.CONNECTED) {
alert("connected");
}else if (status == Strophe.Status.DISCONNECTED) {
console.log("Strophe is disconnected.");
}
}
</script>
<body>
<button onclick="connect();">click</button>
</body>
</html>
答案 0 :(得分:2)
看看你是否正确配置了URL(BOSH_SERVICE),Openfire默认URL是 “127.0.0.1:7070/http-bind/” 最好使用一个控制台,其中显示Strophe将消息发送到XMPP服务器并从服务器接收以进行调试(查看"Exploring the XMPP Protocol: A Debugging Console"),这本书“Professional XMPP - Programming with JavaScript and jQuery”会很有帮助。
基本的例子:
<!DOCTYPE html>
<html>
<head>
<title>Strophe.js Basic Example</title>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'></script>
<script src='strophe.js'></script>
<script>
//URL server openfire, by default 'http://Server-IP:7070/http-bind/'
var BOSH_SERVICE = 'http://bosh.metajack.im:5280/xmpp-httpbind'
var connection = null;
function log(msg)
{
$('#log').append('<div></div>').append(document.createTextNode(msg));
}
function rawInput(data)
{
log('RECV: ' + data);
}
function rawOutput(data)
{
log('SENT: ' + data);
}
function onConnect(status)
{
if (status == Strophe.Status.CONNECTING) {
log('Strophe is connecting.');
} else if (status == Strophe.Status.CONNFAIL) {
log('Strophe failed to connect.');
$('#connect').get(0).value = 'connect';
} else if (status == Strophe.Status.DISCONNECTING) {
log('Strophe is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
log('Strophe is disconnected.');
$('#connect').get(0).value = 'connect';
} else if (status == Strophe.Status.CONNECTED) {
log('Strophe is connected.');
connection.disconnect();
}
}
$(document).ready(function () {
connection = new Strophe.Connection(BOSH_SERVICE);
connection.rawInput = rawInput;
connection.rawOutput = rawOutput;
$('#connect').bind('click', function () {
var button = $('#connect').get(0);
if (button.value == 'connect') {
button.value = 'disconnect';
connection.connect($('#jid').get(0).value, $('#pass').get(0).value, onConnect);
} else {
button.value = 'connect';
connection.disconnect();
}
});
});
</script>
</head>
<body>
<div id='login' style='text-align: center'>
<form name='cred'>
<label for='jid'>JID:</label>
<input type='text' id='jid'>
<label for='pass'>Password:</label>
<input type='password' id='pass'>
<input type='button' id='connect' value='connect'>
</form>
</div>
<hr>
<div id='log'></div>
</body>
</html>
检查服务器设置中是否启用了http绑定。您可以使用Web浏览器导航到“server-ip:7070”来确认。启用HTTP绑定并在该端口侦听(默认为7070)时,会显示类似“Openfire HTTP Binding Service”的消息。
要更改HTTP绑定设置,只需打开服务器设置Web界面,然后导航到“服务器 - >服务器设置 - > HTTP绑定”。