【问题标题】:XMPP auto reconnect using libstropheXMPP 使用 libstrophe 自动重新连接
【发布时间】:2016-04-07 15:57:39
【问题描述】:

使用 libstrophe,当我失去连接时,我可以自动重新连接吗? 我在客户端使用了以下代码:

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("test@domain.com")
    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);
}   

当客户端第一次连接时,我收到消息“DEBUG:已连接” 服务器完成后,我收到消息“调试:已断开连接”。但是当服务器第二次启动时,客户端不会自动重新连接。

【问题讨论】:

    标签: c xmpp libstrophe


    【解决方案1】:

    Libstrophe 不会自动重新连接。从 libstrophe-0.9.0 开始,xmpp_conn_t 对象可以重新连接而不会丢失登录信息和用户处理程序:

    #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  = "test@domain.com";
        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;
    }
    

    在 0.9.0 之前的版本中,用户无法在断开连接后重用 xmpp_conn_t 对象,需要创建一个新对象。 libstrophe-0.8.8 及更早版本的示例:

    #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  = "test@domain.com";
        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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 2021-03-15
      • 2014-05-07
      • 2014-12-03
      相关资源
      最近更新 更多