gaddam nagaraju
gaddam nagaraju

Reputation: 187

Unable to connect to xmpp server in phonegap with openfire server using strophe.js

I am new to the phonegap.Iam using coredova latest version 2.9.0 for developing chatting application by connecting to xmpp server with open fire server..I have been searching for the related storph.js code in phonegap since last 2 days.I didn't get the running code in phonegap,getting status '1' means connecting..can anyone helps...thanks in advace.

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

Upvotes: 1

Views: 1489

Answers (1)

ronielhcuervo
ronielhcuervo

Reputation: 715

See if you have properly configured the URL (BOSH_SERVICE), Openfire default URL is "127.0.0.1:7070/http-bind/" It would be good to use a console where show Strophe sends messages to the XMPP server and receives from the server to debug(look at "Exploring the XMPP Protocol: A Debugging Console"), It can be helpful the book “Professional XMPP - Programming with JavaScript and jQuery”.

Basic strophe example:

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

Check if the http binding is enabled in your server settings. You can confirm that by navigating to "server-ip:7070" with your web browser. A message like this one "Openfire HTTP Binding Service" is shown when the HTTP binding is enabled and listening at that port (7070 is the default).

To change the HTTP binding settings just open the server settings web interface and navigate to "Server->Server Settings->HTTP Binding".

Upvotes: 2

Related Questions