Windowsクライアント開発--xmppプロトコルglooxライブラリ


多くのチャットルームクライアントは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を設定します.
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;
}