windows客户端开发--xmpp协议gloox库
2016-03-30 23:13
369 查看
很多聊天室客户端都使用了xmpp协议。
这个协议被人们诟病很多,但是对于简单的消息传送还是有很大的使用价值的。
XMPP is the open standard for messaging and presence。
gloox is a popular library for the Extensible Messaging and Presence Protocol (XMPP), formerly known as Jabber.
所以写了一个简单的例子,如何使用gloox连接服务器,监听消息,以及如何应答。
首先是设置 MessageHandler:
这里我们创建了一个客户端,有ID和密码。
接下来,我们需要ConnectionListener就处理连接。
而ConnectionListener一定要registered with 客户端。这个一会再说。
现在是连接服务器,我们采用non-bloking的方式。
为了能够处理消息,我们要实现handleMessage方法:
这时候我们需要connectlisenner来处理连接,需要重写纯虚函数:
上面代码重要的是onTLSConnect()函数。
全部代码:
这个协议被人们诟病很多,但是对于简单的消息传送还是有很大的使用价值的。
XMPP is the open standard for messaging and presence。
gloox is a popular library for the Extensible Messaging and Presence Protocol (XMPP), formerly known as Jabber.
所以写了一个简单的例子,如何使用gloox连接服务器,监听消息,以及如何应答。
首先是设置 MessageHandler:
class Bot : public MessageHandler { public: Bot() { JID jid("bot@localhost"); client = new Client( jid, "botpwd" ); connListener = new ConnListener(); client->registerMessageHandler( this ); client->registerConnectionListener(connListener); client->connect(true); }
这里我们创建了一个客户端,有ID和密码。
接下来,我们需要ConnectionListener就处理连接。
而ConnectionListener一定要registered with 客户端。这个一会再说。
现在是连接服务器,我们采用non-bloking的方式。
为了能够处理消息,我们要实现handleMessage方法:
virtual void handleMessage( const Message& stanza, MessageSession* session = 0 ) { cout << "Received message: " << stanza << endl; Message msg(stanza.subtype(), stanza.from(), "Tell me more about " + stanza.body() ); client->send( msg ); }
这时候我们需要connectlisenner来处理连接,需要重写纯虚函数:
class ConnListener : public ConnectionListener { public: virtual void onConnect() { cout << "ConnListener::onConnect()" << endl; } virtual void onDisconnect(ConnectionError e) { cout << "ConnListener::onDisconnect() " << e << endl; } virtual bool onTLSConnect(const CertInfo& info) { cout << "ConnListener::onTLSConnect()" << endl; return true; } };
上面代码重要的是onTLSConnect()函数。
全部代码:
#include <iostream>
#include <string>
#include <gloox/client.h>
#include <gloox/message.h>
#include <gloox/messagehandler.h>
#include <gloox/connectionlistener.h>
using namespace std;
using namespace gloox;
ostream& operator<<(ostream& os, Message::MessageType type) {
switch (type) {
case Message::Chat:
os << "Chat";
break;
case Message::Error:
os << "Error";
break;
case Message::Groupchat:
os << "Groupchat";
break;
case Message::Headline:
os << "Headline";
break;
case Message::Normal:
os << "Normal";
break;
case Message::Invalid:
os << "Invalid";
break;
default:
os << "unknown type";
break;
}
}
ostream& operator<<(ostream& os, const Message& stanza) {
os << "type:'" << stanza.subtype() << "' from:'" << stanza.from().full() << "' body:'" << stanza.body() << "'";
return os;
}
class ConnListener : public ConnectionListener { public: virtual void onConnect() { cout << "ConnListener::onConnect()" << endl; } virtual void onDisconnect(ConnectionError e) { cout << "ConnListener::onDisconnect() " << e << endl; } virtual bool onTLSConnect(const CertInfo& info) { cout << "ConnListener::onTLSConnect()" << endl; return true; } };
class Bot : public MessageHandler { public: Bot() { JID jid("bot@localhost"); client = new Client( jid, "botpwd" ); connListener = new ConnListener(); client->registerMessageHandler( this ); client->registerConnectionListener(connListener); client->connect(true); }
~Bot() {
delete client;
delete connListener;
}
virtual void handleMessage( const Message& stanza, MessageSession* session = 0 ) {
cout << "Received message: " << stanza << endl;
Message msg(Message::Chat, stanza.from(), "Tell me more about " + stanza.body() );
client->send( msg );
}
private:
Client* client;
ConnListener* connListener;
};
int main() {
Bot b;
}
相关文章推荐
- Android 应用发布全攻略
- Android dumpsys命令学习小记
- qt4与qt5不兼容问题
- 欢迎使用CSDN-markdown编辑器
- 使用ThinkPHP上传类处理KindEditor上传的图片
- Android 5.0 parseSdkContent failed Could not initialize class android.graphics.Typeface
- redis(一)简介
- redis(二)redis+TCMALLOC高性能的缓存服务器的安装配置
- 关于Android滑动冲突的解决方法(一)
- MYSQL的常用命令和增删改查语句和数据类型
- redis(二)redis+TCMALLOC高性能的缓存服务器的安装配置
- redis(二)redis+TCMALLOC高性能的缓存服务器的安装配置
- redis(一)简介
- redis(一)简介
- MongoDB权限认证
- 我的2016年决心书
- 基于Docker快速搭建多节点Hadoop集群
- java SE基础(Map接口及其实现)
- 解决HashMap并发问题
- 小雨 20℃ 3.30日