libstrophe发送图片

今天用libstrophe做了一个发送图片的,自己用了三个方法进行实现:感觉可能还是这个最好吧!之前两个CTO不想增加项目大小,被否决。这是用于将windows上的图片发送到ios手机项目中,所以需要自己去实现下。用过libevent、gloox库。下面是libstrophe库的实现。

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "base64.h"

#include <strophe.h>

#include <iostream>

#include <fstream>

struct msgdata

{

char* from;

char* to;

//long length;

char* msg;

};

/** Send a single text message.

*  This function sends a simple text message to a given recipient.

*

*  @param conn a Strophe connection object

*  @param to the JID of the recipient

*  @param message the text of the message to send

*

*  @ingroup Connections

*/

void xmpp_send_simple_message(xmpp_conn_t* conn,const char* const from,const char* const to,const char* const message)

{

xmpp_stanza_t *body,*text,*command;

xmpp_ctx_t *ctx = xmpp_conn_get_context(conn);

char* rePath = "aaa.jpg";

command = xmpp_stanza_new(ctx);

xmpp_stanza_set_name(command,"message");

xmpp_stanza_set_type(command,"file");

xmpp_stanza_set_attribute(command,"from",from);

xmpp_stanza_set_attribute(command,"to",to);

xmpp_stanza_set_attribute(command,"destin","/");

body = xmpp_stanza_new(ctx);

xmpp_stanza_set_name(body,"body");

xmpp_stanza_t *item =xmpp_stanza_new(ctx);

xmpp_stanza_set_name(item,"item");

xmpp_stanza_set_attribute(item, "path", rePath);

text = xmpp_stanza_new(ctx);

xmpp_stanza_set_text(text,message);

//xmpp_stanza_add_child(body,text);

xmpp_stanza_add_child(item,text);

xmpp_stanza_add_child(body,item);

xmpp_stanza_add_child(command,body);

xmpp_send(conn,command);

xmpp_stanza_release(command);

}

/*define a handler for connection events*/

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_conn_get_context(conn);

struct msgdata* msg = (struct msgdata*)userdata;

if(status ==XMPP_CONN_CONNECT)

{

xmpp_stanza_t* pres;

fprintf(stderr,"DEBUG:connected\n");

/*send initial <presence/> so that we appear online to contacts*/

pres = xmpp_stanza_new(ctx);

//xmpp_handler_add(conn,presence_handler, NULL, "presence", NULL, ctx);

xmpp_stanza_set_name(pres,"presence");

xmpp_send(conn,pres);

xmpp_stanza_release(pres);

/*send a message*/

xmpp_send_simple_message(conn,msg->from,msg->to,msg->msg);

free(msg);

/*cause the conn_handler to be executed,where the event loop gets shut down*/

xmpp_disconnect(conn);

}else

{

fprintf(stderr,"DEBUG:disconnect\n");

xmpp_stop(ctx);

}

}

int main(int argc,char** argv)

{

xmpp_ctx_t *ctx;

xmpp_conn_t *conn;

xmpp_log_t *log;

char *jid,*pass;

struct msgdata* msg = (struct msgdata*)malloc(sizeof(struct msgdata));

msg->to=  "[email protected]/";

msg->from= "[email protected]/";

jid =msg->from;

pass = "test";

/*initalize library*/

xmpp_initialize();

/*create a context*/

log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);

ctx = xmpp_ctx_new(NULL,log);

/* create a connection*/

conn = xmpp_conn_new(ctx);

/* setup authentication information*/

xmpp_conn_set_jid(conn,jid);

xmpp_conn_set_pass(conn,pass);

/* prepare the message for sending*/

std::ifstream file;

file.open("C:/Users/Administrator/Desktop/2.jpg",std::ios_base::in|std::ios_base::binary|std::ios_base::ate);

file.seekg(0,std::ios::end);

char *buffer ;

long size;

size = file.tellg();

buffer = new char [size];

file.seekg(0,std::ios::beg);

file.read (buffer, size);

std::string picture = base64_encode(reinterpret_cast<const unsigned char*>(buffer), size);

msg->msg = const_cast<char*>(picture.c_str());

/* initialize connection*/

xmpp_connect_client(conn,NULL,0,conn_handler,msg);

/* enter the event loop - our connect handler will trigger an exit*/

xmpp_run(ctx);

/*release our connection and contex*/

xmpp_conn_release(conn);

xmpp_ctx_free(ctx);

/*final shutdown of the libraty*/

xmpp_shutdown();

return 0;

}

libstrophe发送图片

时间: 2024-08-28 20:12:22

libstrophe发送图片的相关文章

在类似qq或者微信聊天中。如何根据不同的手机发送图片

原文:在类似qq或者微信聊天中.如何根据不同的手机发送图片   前一段时间,公司自己要求做多客服开发,但是对于发送图片这一块,当时很苦恼,我用自己的手机(米2)测试,不管是本地,还是云相册,最新照片.都没有问题,但是测试那边一直说图片发不了,而且还会崩.很纳闷.      后来经过debug,发现4.4以上的手机,它的图片路径居然不一样,有file://开头的,也有content://开头的,还有/mnt/sdcard/开头的,坑爹啊,我自己的手机是4.1的,不一样.     4.4以前的路径都

微信企业号:shell定时发送图片 到 指定人

#!/bin/bash #返回access token function getToken(){ #传入参数$1为corpid,参数$2为corpsecret     curl -s "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$1&corpsecret=$2" | awk -F '"' '{print $4}' } #返回media_id function getMediaId(){ #传入参数$1为ac

使用websocket发送图片文件

Websocket是HTML5中的一个传输数据控件. 眼下在主流的浏览器中都提供了支持. 关于websocket的相关协议能够在网上查到,关于怎样建立连接等操作大家能够查询协议获得.本文主要讨论怎样进行传输数据. 在client使用的是Chrome浏览器,在server端使用PHP开发websocket协议. Websocket协议一个frame的标准格式: 0                   1                   2                   3 0 1 2 3

weChat聊天发送图片带有小尖角的实现

weChat聊天发送图片带有小尖角的实现 1.#import <UIKit/UIKit.h>2.3.@interface JKShapeImage : UIView4.5.@property (nonatomic,strong) UIImage *image;6.7.@property (nonatomic,getter=isOnLeft,assign) BOOL onLeft;8.9.@end 1.#import "JKShapeImage.h"2.3.@interfac

H5+MUI+Node.js+Socket.io实现即时聊天以及发送+图片压缩

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <title></title

用libevent实现的发送图片

服务器端的代码: #include <event2/event.h> #include <event2/event_compat.h> #include <event2/event_struct.h> #include <map> #include <vector> #include <string> #include <iostream> #ifndef _WIN32 #include <fcntl.h> #

从服务器 (PC 端 ) 发送图片到客户端 (android 手机端 ) [转]

从服务器 (PC 端 ) 发送图片到客户端 (android 手机端 ) ,并在手机页面上显示该图片.(注:本文旨在实现功能,并未考虑效率,有待后续跟进.) 1.服务器端 1 int port=9090; 2 3 4 /** 5 * 发送文件的方法 6 * 此处定义服务器端口为9090,ip地址为192.168.1.1 7 * 设定被传输图片的路径为"images/icon.png" 8 * images文件夹放在此工程的根目录下,我们就可以通过相对路径访问这个图片文件了 9 */ 1

微信 不能发送图片 WeChat1.5.rar

这两天撞见一个很奇葩的问题,升级weixin 2.2版本竟然不能发送图片!!! 解决办法 很笨:退一步海阔天空   !回退半步 卸新的装旧的  so easy ! 想要回退的朋友自行下载 WeChat1.5.rar http://down.51cto.com/data/2249373

php://input解决APP发送图片问题

今天公司要求用APP发送一个图片到PHP程序接收并保存起来,而且中间还需要很多参数! 以前没有做过APP和PHP交互,这次算是一个挑战吧(对一个没有人指导实习生来说) 1.APP发1.jpg,而且带有两个参数一个是假设是X和另外一个假设是Y 2.PHP负责接受X,Y和1.jpg,并且还要保存1.jpg到服务器 步骤: 1.PHP页面代码 $data = file_get_contents(‘php://input’);//这样可以获取到未经处理的原数据(保持发送的图片流不被破坏),在APP上使用