查看您是否正确配置了 URL (BOSH_SERVICE),Openfire 默认 URL 是“127.0.0.1:7070/http-bind/”最好使用控制台,其中 show Strophe 将消息发送到 XMPP 服务器并从服务器接收调试(查看“探索 XMPP 协议:调试控制台”),这本书对“<a href="http://professionalxmpp.com" rel="nofollow">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 绑定服务”的消息。
要更改 HTTP 绑定设置,只需打开服务器设置 Web 界面并导航到“服务器->服务器设置->HTTP 绑定”。