1

我是 phonegap 的新手。我使用 coredova 最新版本 2.9.0 通过使用 open fire 服务器连接到 xmpp 服务器来开发聊天应用程序。自过去 2 天以来,我一直在 phonegap 中搜索相关的 storph.js 代码。我没有't get the running code in phonegap, get status '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>
4

1 回答 1

2

查看您是否正确配置了 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 绑定”。

于 2013-08-28T15:45:30.827 回答