Gloox send仅在线程睡眠时起作用。或不?

时间:2019-04-08 06:27:16

标签: c++ gloox

我正在尝试为xmpp写一个简单的消息发件人。决定利用gloox。因此,事实证明,没有手动线程睡眠消息就会丢失。问题,如果有人知道如何使gloox等待发送消息然后退出?

示例代码:

class MyMessenger: public ConnectionListener {
public:
    MyMessenger(): client( 0 ) {}

    ~MyMessenger() {
        if( client ) delete( client );
    }

    virtual void onConnect() override {
        JID to(DST_JID);
        MessageSession* session = new MessageSession( client, to );
        session->send("test message");
        // Without this message is lost.
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
        client->disposeMessageSession(session);
        client->disconnect();
    };

    virtual void onDisconnect( ConnectionError ce ) override {
        if( ce == ConnAuthenticationFailed )
            cerr << "Auth failed. Reason: " << client->authError() << endl;
    }

    virtual bool onTLSConnect( const CertInfo& ) override {
        return true;
    }

    void start() {

        JID jid( USER_JID );

        client = new Client( jid, USER_PASS );

        client->setServer( SERVER );
        client->setPort( PORT );
        client->setTls( TLSRequired );

        client->registerConnectionListener( this );

        if( client->connect( false ) ) {
            ConnectionError ce = ConnNoError;

            while( ce == ConnNoError ) {
                ce = client->recv();
            }
            cerr << "ce: " << ce << endl;
        }

    }

private:
    Client* client;
};

0 个答案:

没有答案