C调用libstrophe库



int xmpp_send_message(xmpp_conn_t * const conn, xmpp_ctx_t *ctx, char *data)
{
xmpp_stanza_t *reply, *properties;

reply = xmpp_message_new(ctx, "chat", node_strophe.to, xmpp_stanza_get_id(xmpp_stanza_new(ctx)));
xmpp_message_set_body(reply, data);
xmpp_send(conn, reply);
xmpp_stanza_release(reply);


return 1;
}

int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
    xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
    xmpp_stanza_t *reply;
    char *intext ;


if( xmpp_stanza_get_type(stanza) != NULL && !strcmp(xmpp_stanza_get_type(stanza), "error"))
{
intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));
fprintf(stderr,"recv xmpp server error [%s]", intext);
}
       else
      {
   if (!xmpp_stanza_get_child_by_name(stanza, "body"))
   {
xmpp_disconnect(conn);
return 1;
   }

        intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));
    if(intext != NULL)
    {
fprintf(stderr, "recv xmpp server data[%s] \n", intext) ;
xmpp_free( ctx, intext );
intext = NULL;
    }
    }


    return 1;
}

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)
{
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
if(status == XMPP_CONN_CONNECT)
{
xmessage("DEBUG: conn_handler connected \n");
xmpp_stanza_t *pres;
              xmpp_handler_add(conn, message_handler, NULL, "message", NULL, ctx);
pres = xmpp_presence_new(ctx);
xmpp_send(conn, pres);
xmpp_stanza_release(pres);

while(1)
{
xmpp_send_message(conn, ctx, "hello");
usleep(5000);
}
}
else{
xmpp_stop(ctx);
}

}

void *sub_xmpp_connect(void *arg, char* jid, char* pass,char *ip)
{
xmpp_ctx_t *ctx;
        xmpp_conn_t *conn;
        xmpp_log_t *log;
int ret = 0;

xmpp_initialize();
node_strophe->log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); 

node_strophe->ctx = xmpp_ctx_new(NULL, node_strophe->log);
node_strophe->conn = xmpp_conn_new(node_strophe->ctx);
xmpp_conn_set_flags(node_strophe->conn, flags);

xmpp_conn_set_jid(node_strophe->conn, node_strophe->jid);
xmpp_conn_set_pass(node_strophe->conn, node_strophe->pass);

ret = xmpp_connect_client(conn, ip, 5222, conn_handler, ctx);
if(ret < 0)
{
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);
xmpp_shutdown();
}
xmpp_run(node_strophe->ctx);


return NULL;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值