Looking for example of how to properly reconnect after a disconnect

68 views
Skip to first unread message

Brian Jett

unread,
Sep 16, 2013, 2:26:54 PM9/16/13
to ja...@googlegroups.com
Hello all,

I've built an autonomous jaxl-based client that runs in the background as a service on a windows-based kiosk to enable communicating with processes running on the kiosk.  Everything works great, but I'm testing what happens when the client is disconnected from the server (in my case, an instance of Openfire).  I'm currently getting around this by having a second process that checks for the PID of the service and restarts it if it's not running (effective, but not elegant).

Additionally, when the client disconnects on it's own (for instance, it downloaded an updated code base and is exiting to restart with the new code), I see from the Openfire console that the client is disconnected, but to other clients the kiosk still appears connected.  I've tried setting the status to away prior to disconnecting but with out luck.  I've not tested on other XMPP servers so I don't know if this is Openfire-specific or not.

Here's the relevant code (adapted from one of the example scripts):

<?php

define('VERSION', '1.1.36');

require_once 'JAXL-3.x/jaxl.php';
error_reporting(E_ALL);
set_error_handler("customError");

/* determine our jabber id */
$hostname = strtolower(gethostname());
define('JID', 'kiosk.' . $hostname . '@jabber.XXXXXXXXXXX.com');
define('XMPP_HOST', 'XXXXXXXXXXXX');
define('XMPP_PORT', 5222);

define('PHP_PATH', 'c:\xampplite\php\php.exe');
define('DGB_JABBER_PATH', 'c:\xampplite\htdocs\jabber');
define('TEMP_DIR', 'c:\xampplite\tmp\processing');
define('LOG_DIR', 'c:\xampplite\tmp\logs');
define('LOG_FILE', LOG_DIR . '\jabber-' . date("Y-m-d") . '.log');
define('IMS_ENTITY_NAME', _getHostname());
define('IMS_PASSWORD', 'XXXXXXXXXXXXXX');
define('CDS_PREAMBLE', DGB_CDS_PATH . '\index.php agent ');
define('VERBOSE_FILE', DGB_CDS_PATH . '\verbose.txt');
define('START_TS', time());
define('PID_FILE', LOG_DIR . '\jabber_pid.txt');

... SNIP ...

_log("Starting CDS Jabber Client version " . VERSION);
_log("Logging in as " . JID);

$client = new JAXL(array(
'jid' => JID,
'pass' => IMS_PASSWORD,
'host' => XMPP_HOST,
'port' => XMPP_PORT,
'auth_type' => 'PLAIN',
'log_level' => JAXL_WARNING
));
$client->require_xep(array('0199'));  /* XMPP Ping */

/* Define callbacks for various events */
$client->add_cb('on_auth_success', function() {
global $client;
_log('Authorized: ' . $client->full_jid->to_string());
$client->get_roster();
$client->set_status("chat", "available", 1);
});

$client->add_cb('on_auth_failure', function($reason) {
global $client;
_log('Auth FAILURE: ' . $reason);
$client->send_end_stream();
throw new Exception('Failed to authenticate: ' . $reason);
});

$client->add_cb('on_normal_message', function($stanza) {
if ($stanza->body) {
_log("New Normal Message from " . $stanza->from . ': ' . $stanza->body);
handleMessage($stanza);
}
});

$client->add_cb('on_chat_message', function($stanza) {
if ($stanza->body) {
_log("New Message from " . $stanza->from . ': ' . $stanza->body);
handleMessage($stanza);
}
});

$client->add_cb('on_groupchat_message', function($stanza) {
if ($stanza->body) {
_log("New Group Message from " . $stanza->from . ': ' . $stanza->body);
handleMessage($stanza);
}
});

$client->add_cb('on_presence_stanza', function($stanza) {
$type = ($stanza->type ? $stanza->type : "available");
$show = ($stanza->show ? $stanza->show : "???");
_info($stanza->from." is now " . $type . " ($show)");
});

$client->add_cb('on_disconnect', function() {
global $client;
_log("Uh oh, I got disconnected, exiting ...");
throw new Exception('Disconnect');

});

$client->add_cb('on_connect_failure', function() {
global $client;
_log("Uh oh, failed to connect to remote server, exiting ...");
throw new Exception('Connect Failure');

});

/* get this party started ... */
try {
$client->start(array(
'--with-debug-shell' => false,
'--with-unix-sock' => false
));
} catch (Exception $e) {
$client->send_end_stream();
_log("Client exiting after " . round((time() - START_TS)/3600, 1) . " hours, reason is " . $e->getMessage());
global $LOG_HANDLE;
fclose($LOG_HANDLE);
exit(0);
} 

... the rest of the app ...

I've tried to call $client->start() again in the on_disconnect callback but the client just hangs.  Thanks in advance for any guidance.

Brian
Reply all
Reply to author
Forward
0 new messages