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;
}
C调用libstrophe库
最新推荐文章于 2020-07-08 17:33:29 发布