[{"data":1,"prerenderedAt":81},["ShallowReactive",2],{"\u002F2014-08-04-xmpp%E5%AE%A2%E6%88%B7%E7%AB%AFc%E5%BA%93gloox%E7%AE%80%E6%98%8E%E6%A6%82%E8%A6%81":3},{"id":4,"title":5,"body":6,"date":56,"description":12,"extension":57,"meta":58,"navigation":61,"path":73,"seo":74,"stem":75,"tags":76,"__hash__":80},"blogs\u002F_legacy\u002F2014\u002F2014-08-04-xmpp%e5%ae%a2%e6%88%b7%e7%ab%afc%e5%ba%93gloox%e7%ae%80%e6%98%8e%e6%a6%82%e8%a6%81.md","XMPP客户端C++库Gloox简明概要",{"type":7,"value":8,"toc":52},"minimark",[9,13,16,19,29,32,43,46,49],[10,11,12],"p",{},"Gloox是Xmpp协议客户端的Lib，使用起来非常简单。官方自带的sample也非常的实用，功能方面恰到好处的没有提供显示之类的。对于开发xmpp的bot而言是非常的合适。",[10,14,15],{},"Xmpp协议虽然不是很流行，但是基于Xmpp的聊天服务还是有很多优点的。使用Xmpp协议来制作bot可以实现很多功能，只要不是网络游戏那样实时大量数据包传递的话，Xmpp是一个很好的避开与网络交互底层接触的解决方案。",[10,17,18],{},"Xmpp的协议详细的可以参照RFC文档，不过既然选择使用库来做的话就是懒得和协议上的东西打交道。基本功能上就是个聊天软件，想要详细接触的童鞋可以下载Pidgin尝试一下，GTalk是可以使用Xmpp协议连接的。",[10,20,21,22],{},"Gloox的设计简单易懂，基于事件驱动。官方有个详细的说明页面，这位前辈的博客上贴出了翻译：",[23,24,28],"a",{"href":25,"rel":26},"http:\u002F\u002Fblog.csdn.net\u002Fkkaxiao\u002Farticle\u002Fdetails\u002F6702298%E3%80%82%E6%9C%89%E5%85%B4%E8%B6%A3%E8%AF%A6%E7%BB%86%E4%BA%86%E8%A7%A3%E7%9A%84%E7%AB%A5%E9%9E%8B%E5%8F%AF%E4%BB%A5%E5%8E%BB%E7%9C%8B%E7%9C%8B%E3%80%82",[27],"nofollow","http:\u002F\u002Fblog.csdn.net\u002Fkkaxiao\u002Farticle\u002Fdetails\u002F6702298。有兴趣详细了解的童鞋可以去看看。",[10,30,31],{},"如果只是做简单的开发的话，基本上不会需要很深入的了解。等需要的时候再针对性的找找相关说明即可，gloox的文档里面可以找到大多数的答案。Gloox的使用可以简单的分为两个步骤：登陆账号、处理消息。",[33,34,39],"pre",{"className":35,"code":37,"language":38},[36],"language-text","#include \"gloox\u002Fclient.h\"\n#include \"gloox\u002Fmessagesessionhandler.h\"\n#include \"gloox\u002Fmessageeventhandler.h\"\n#include \"gloox\u002Fmessageeventfilter.h\"\n#include \"gloox\u002Fchatstatehandler.h\"\n#include \"gloox\u002Fchatstatefilter.h\"\n#include \"gloox\u002Fconnectionlistener.h\"\n#include \"gloox\u002Fdisco.h\"\n#include \"gloox\u002Fmessage.h\"\n#include \"gloox\u002Fgloox.h\"\n#include \"gloox\u002Flastactivity.h\"\n#include \"gloox\u002Floghandler.h\"\n#include \"gloox\u002Flogsink.h\"\n#include \"gloox\u002Fconnectiontcpclient.h\"\n#include \"gloox\u002Fconnectionsocks5proxy.h\"\n#include \"gloox\u002Fconnectionhttpproxy.h\"\n#include \"gloox\u002Fmessagehandler.h\"\nusing namespace gloox;\n\n#ifndef _WIN32\n# include \u003Cunistd.h>\n#endif\n\n#include \u003Cstdio.h>\n#include \u003Cstring>\n\n#include \u003Ccstdio> \u002F\u002F [s]print[f]\n\n#if defined( WIN32 ) || defined( _WIN32 )\n# include \u003Cwindows.h>\n#endif\n\nclass MessageTest : public MessageSessionHandler, ConnectionListener, LogHandler,\n    MessageEventHandler, MessageHandler, ChatStateHandler\n{\npublic:\n    MessageTest() : m_session( 0 ), m_messageEventFilter( 0 ), m_chatStateFilter( 0 ) {}\n\n    virtual ~MessageTest() {}\n\n    void start()\n    {\n        \u002F\u002F \u003CID和密码 \n        JID jid( \"username@gmail.com\u002Fgloox\" );  \u002F\u002F \u003C Id后面的\u002Fgloox是资源名称，并不是必须的，Xmpp支持多个客户端同时接入，可以用作区分。\n        j = new Client( jid, \"password\" );\n        \n        \u002F\u002F \u003C注册事件处理\n        j->registerConnectionListener( this );\n        j->registerMessageSessionHandler( this, 0 );\n        j->disco()->setVersion( \"messageTest\", GLOOX_VERSION, \"Win32\" );\n        j->disco()->setIdentity( \"client\", \"bot\" );\n        j->disco()->addFeature( XMLNS_CHAT_STATES );\n        StringList ca;\n        ca.push_back( \"\u002Fpath\u002Fto\u002Fcacert.crt\" );\n        j->setCACerts( ca );\n        j->logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, this );\n\n\n        if( j->connect( false ) )\n        {\n            ConnectionError ce = ConnNoError;\n            while( ce == ConnNoError )\n            {\n                ce = j->recv();\n            }\n            printf( \"ce: %d\\n\", ce );\n        }\n\n        delete( j );\n    }\n\n    virtual void onConnect()\n    {\n        printf( \"connected!!!\\n\" );\n    }\n\n    virtual void onDisconnect( ConnectionError e )\n    {\n        printf( \"message_test: disconnected: %d\\n\", e );\n        if( e == ConnAuthenticationFailed )\n            printf( \"auth failed. reason: %d\\n\", j->authError() );\n    }\n\n    virtual bool onTLSConnect( const CertInfo& info )\n    {\n        time_t from( info.date_from );\n        time_t to( info.date_to );\n\n        printf( \"status: %d\\nissuer: %s\\npeer: %s\\nprotocol: %s\\nmac: %s\\ncipher: %s\\ncompression: %s\\n\"\n            \"from: %s\\nto: %s\\n\",\n            info.status, info.issuer.c_str(), info.server.c_str(),\n            info.protocol.c_str(), info.mac.c_str(), info.cipher.c_str(),\n            info.compression.c_str(), ctime( &from ), ctime( &to ) );\n        return true;\n    }\n\n    virtual void handleMessage( const Message& msg, MessageSession * \u002F*session*\u002F )\n    {\n        printf( \"type: %d, subject: %s, message: %s, thread id: %s\\n\", msg.subtype(),\n            msg.subject().c_str(), msg.body().c_str(), msg.thread().c_str() );\n\n        std::string re = \"You said:\\n> \" + msg.body() + \"\\nI like that statement.\";\n        std::string sub;\n        if( !msg.subject().empty() )\n            sub = \"Re: \" +  msg.subject();\n\n        m_messageEventFilter->raiseMessageEvent( MessageEventDisplayed );\n#if defined( WIN32 ) || defined( _WIN32 )\n        Sleep( 1000 );\n#else\n        sleep( 1 );\n#endif\n        m_messageEventFilter->raiseMessageEvent( MessageEventComposing );\n        m_chatStateFilter->setChatState( ChatStateComposing );\n#if defined( WIN32 ) || defined( _WIN32 )\n        Sleep( 2000 );\n#else\n        sleep( 2 );\n#endif\n        m_session->send( re, sub );\n\n        if( msg.body() == \"quit\" )\n            j->disconnect();\n    }\n\n    virtual void handleMessageEvent( const JID& from, MessageEventType event )\n    {\n        printf( \"received event: %d from: %s\\n\", event, from.full().c_str() );\n    }\n\n    virtual void handleChatState( const JID& from, ChatStateType state )\n    {\n        printf( \"received state: %d from: %s\\n\", state, from.full().c_str() );\n    }\n\n    virtual void handleMessageSession( MessageSession *session )\n    {\n        printf( \"got new session\\n\");\n        \u002F\u002F \u003C示例程序只处理一个Session，所以在这里释放掉前一个\n        j->disposeMessageSession( m_session );\n        m_session = session;\n        m_session->registerMessageHandler( this );\n        m_messageEventFilter = new MessageEventFilter( m_session );\n        m_messageEventFilter->registerMessageEventHandler( this );\n        m_chatStateFilter = new ChatStateFilter( m_session );\n        m_chatStateFilter->registerChatStateHandler( this );\n    }\n\n    virtual void handleLog( LogLevel level, LogArea area, const std::string& message )\n    {\n        printf(\"log: level: %d, area: %d, %s\\n\", level, area, message.c_str() );\n    }\n\nprivate:\n    Client *j;\n    MessageSession *m_session;\n    MessageEventFilter *m_messageEventFilter;\n    ChatStateFilter *m_chatStateFilter;\n};\n\nint main( int \u002F*argc*\u002F, char** \u002F*argv*\u002F )\n{\n    MessageTest *r = new MessageTest();\n    r->start();\n    delete( r );\n    getchar();getchar();\n    return 0;\n}\n\n","text",[40,41,37],"code",{"__ignoreMap":42},"",[10,44,45],{},"以上代码来自官方的Sample，主要功能是对接到的文字进行复述。在这个基础上进行修改就可以实现常见的需求了。",[10,47,48],{},"在使用gloox的过程中有一点需要注意的是，MessageSession如果保存了其指针的话，不要自行进行Delete操作，而应该交由Client来释放。j->disposeMessageSession( m_session )这样的做法才是正确的。",[10,50,51],{},"由于不是做客户端，实际上做的工作还是比较少的。但是刚上手一个库的时候多少会有些不知所措，希望对有用到的童鞋起到帮助。",{"title":42,"searchDepth":53,"depth":54,"links":55},2,3,[],"2014-08-04","md",{"layout":59,"status":60,"published":61,"author":62,"author_login":63,"author_email":64,"author_url":65,"wordpress_id":66,"wordpress_url":67,"date_gmt":68,"excerpt":69},"post","publish",true,{"display_name":63,"login":63,"email":64,"url":65},"chaoshikari","chaoshikari@gmail.com","\u002F",936,"\u002F\u002F?p=936","2014-08-04 12:00:40 +0000",{"type":7,"value":70},[71],[10,72,12],{},"\u002F2014-08-04-xmpp客户端c库gloox简明概要",{"title":5,"description":12},"_legacy\u002F2014\u002F2014-08-04-xmpp%e5%ae%a2%e6%88%b7%e7%ab%afc%e5%ba%93gloox%e7%ae%80%e6%98%8e%e6%a6%82%e8%a6%81",[77,78,79],"c++","XMPP","Gloox","8Pmh-CAzgjTKCb-L_s5XiwTQT1K9fmwQtV1LkzzZA8Q",1788763181998]