基于loudmouth的XMPP客户端DEMO

假如想象 提交于 2020-02-29 20:59:44

这几天在公司一直在研究XMPP客户端软件DEMO的编写,下面的源代码是基于开源库loudmouth-1.4.3的XMPP客户端源代码。这段代码只是一个Demo,使用一个已经在服务器上注册了的用户登录到XMPP服务器,最后在XMPP服务器上发送iq信息,在客户端获取该IQ信息后解析该信息。

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <loudmouth.h>

 

#define XMPP_SERVER "192.168.175.211"

#define XMPP_USERNAME "tm"

#define XMPP_PASSWORD "12345"

#define XMPP_RESOURCE "AndroidpnClient"

 

LmHandlerResult iq_handler_message(LmMessageHandler *handler, LmConnection *connection, LmMessage *m, gpointer user_data)

{

LmMessageNode *root_node, *node;

 

printf("[RECEIVED]:%s\n", lm_message_node_to_string(lm_message_get_node(m)));

root_node = lm_message_get_node(m);

node = lm_message_node_find_child(root_node, "title");

printf("title = %s\n", lm_message_node_get_value(node));

node = lm_message_node_find_child(root_node, "message");

printf("message = %s\n", lm_message_node_get_value(node));

node = lm_message_node_find_child(root_node, "uri");

printf("uri = %s\n", lm_message_node_get_value(node));

lm_message_node_unref(node);

lm_message_node_unref(root_node);

return LM_HANDLER_RESULT_REMOVE_MESSAGE;

}

 

 

int main(int argc, char **argv)

{

LmConnection *connection;

LmSSL *ssl;

GError *error = NULL;

LmMessage *m, *child_m, *response_m;

LmMessageNode *m_node, *child_node, *child;

gboolean use_sasl;

int i;

LmMessageHandler *iq_handler;

GMainLoop        *main_loop;

//create a new closed connection

connection = lm_connection_new(XMPP_SERVER);

if (connection == NULL) {

printf("lm_connection_new(%s) error.\n", XMPP_SERVER);

return -1;

}

//connection uses starttls 

ssl = lm_ssl_new(NULL, NULL, NULL, NULL);

lm_ssl_use_starttls(ssl, TRUE, FALSE);

lm_connection_set_ssl(connection, ssl);

//register iq message handler to the connection 

iq_handler = lm_message_handler_new(iq_handler_message, NULL, NULL);

lm_connection_register_message_handler (connection, iq_handler, LM_MESSAGE_TYPE_IQ, LM_HANDLER_PRIORITY_NORMAL);

lm_message_handler_unref(iq_handler);

//open the connection to create socket with server

if (!lm_connection_open_and_block(connection, &error)) {

g_error("Failed to open:%s\n", error->message);

}

//check the open state of connection

if (lm_connection_is_open(connection)) {

printf("connection to %s [OPENED].\n", XMPP_SERVER);

} else {

printf("connection to %s [FAILED].\n", XMPP_SERVER);

}

//authenticate the specific user/password/resource to the server

if (!lm_connection_authenticate_and_block(connection, XMPP_USERNAME, XMPP_PASSWORD, XMPP_RESOURCE, &error)) {

g_error("Failed to authenticate:%s\n", error->message);

}

//check the authentication state of connection

if (lm_connection_is_authenticated(connection)) {

printf("[%s/%s/%s] is AUTHENTICATION.\n", XMPP_USERNAME, XMPP_PASSWORD, XMPP_RESOURCE);

} else {

printf("[%s/%s/%s] is AUTHENTICATION FAILED.\n", XMPP_USERNAME, XMPP_PASSWORD, XMPP_RESOURCE);

}

//send presence message to server to indicate online 

m=lm_message_new(NULL, LM_MESSAGE_TYPE_PRESENCE);

if (!lm_connection_send(connection, m, &error)) {

printf("Failed to send:%s\n", error->message);

}

lm_message_unref(m);

//gogot glib main loop to receive server push message 

main_loop = g_main_loop_new (NULL, FALSE);

g_main_loop_run (main_loop);

//close connection

lm_connection_close(connection, NULL);

lm_connection_unref(connection);

return 0;

}


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!