XMPP auto reconnect using libstrophe

681 Views Asked by At

using libstrophe, can I reconnect automatically when I loose connection. I used the following code on the client side:

void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
const int error, xmpp_stream_error_t * const stream_error,
void * const userdata)
{
    if (status == XMPP_CONN_CONNECT) {
        fprintf(stderr, "DEBUG: connected\n");      
    }
    else {
        fprintf(stderr, "DEBUG: disconnected\n");
    }
}
void main()
{
    xmpp_log_t  *log;
    char        *jid;       
    jid = strdup("[email protected]")
    xmpp_initialize();
    log = xmpp_get_default_logger(XMPP_LEVEL_ERROR);    
    cwmp->xmpp_ctx = xmpp_ctx_new(NULL, log);
    cwmp->xmpp_conn = xmpp_conn_new(cwmp->xmpp_ctx);
    xmpp_conn_set_jid(cwmp->xmpp_conn, jid);
    xmpp_conn_set_pass(cwmp->xmpp_conn, cwmp->xmpp_param.password);
    xmpp_connect_client(cwmp->xmpp_conn, NULL, 0, conn_handler, cwmp->xmpp_ctx);
xmpp_run(cwmp->xmpp_ctx);
}   

when the client is connected for the first time, i get the message "DEBUG: connected" When the server goes done, i get the message "DEBUG: disconnected". but when the server is up for the second time, the client is not reconnected automatically.

1

There are 1 best solutions below

0
On

Libstrophe doesn't reconnect automatically. Starting from libstrophe-0.9.0 xmpp_conn_t object can be reconnected without loosing login information and user handlers:

#include <stdio.h>
#include <strophe.h>

void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
                  const int error, xmpp_stream_error_t * const stream_error,
                  void * const userdata)
{
    if (status == XMPP_CONN_CONNECT) {
        fprintf(stderr, "DEBUG: connected\n");
    } else {
        fprintf(stderr, "DEBUG: disconnected, reconnecting...\n");
        xmpp_connect_client(conn, NULL, 0, conn_handler, userdata);
    }
}

int main()
{
    xmpp_log_t  *log;
    xmpp_ctx_t  *ctx;
    xmpp_conn_t *conn;
    const char  *jid  = "[email protected]";
    const char  *pass = "password";

    xmpp_initialize();
    log = xmpp_get_default_logger(XMPP_LEVEL_ERROR);
    ctx = xmpp_ctx_new(NULL, log);
    conn = xmpp_conn_new(ctx);
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);
    xmpp_connect_client(conn, NULL, 0, conn_handler, NULL);
    xmpp_run(ctx);

    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);
    xmpp_shutdown();

    return 0;
}

In versions before 0.9.0 user can't reuse xmpp_conn_t object after disconnection and needs to create new one. Example for libstrophe-0.8.8 and older:

#include <stdio.h>
#include <strophe.h>

#define TIMEOUT 1000

void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
                  const int error, xmpp_stream_error_t * const stream_error,
                  void * const userdata)
{
    int *reconnect = userdata;

    if (status == XMPP_CONN_CONNECT) {
        fprintf(stderr, "DEBUG: connected\n");
    } else {
        fprintf(stderr, "DEBUG: disconnected, reconnecting...\n");
        *reconnect = 1;
    }
}

int main()
{
    xmpp_log_t  *log;
    xmpp_ctx_t  *ctx;
    xmpp_conn_t *conn;
    const char  *jid  = "[email protected]";
    const char  *pass = "password";
    int          reconnect;

    xmpp_initialize();
    log = xmpp_get_default_logger(XMPP_LEVEL_ERROR);
    ctx = xmpp_ctx_new(NULL, log);
    while (1) {
        conn = xmpp_conn_new(ctx);
        xmpp_conn_set_jid(conn, jid);
        xmpp_conn_set_pass(conn, pass);
        xmpp_connect_client(conn, NULL, 0, conn_handler, &reconnect);
        reconnect = 0;
        while (!reconnect)
            xmpp_run_once(ctx, TIMEOUT);
        xmpp_conn_release(conn);
    }
    xmpp_ctx_free(ctx);
    xmpp_shutdown();

    return 0;
}