我无法在本地 ejabberd 服务器上连接 BeagleBone Black 上的 gloox 库,这是我的代码,我不断收到错误 9。
#include <gloox/client.h>
#include <gloox/connectionlistener.h>
#include <gloox/messagehandler.h>
#include <gloox/message.h>
#include <iostream>
#include <unistd.h> // Pentru usleep()
using namespace gloox;
class MyMessageHandler : public MessageHandler {
public:
virtual void handleMessage(const Message &msg, MessageSession *session) {
std::cout << "Message from: " << msg.from().bare() << " : " << msg.body() << std::endl;
}
};
class MyConnectionListener : public ConnectionListener {
public:
MyConnectionListener(Client& client) : m_client(client), m_connected(false) {}
virtual void onConnect() {
std::cout << "Connected successfully" << std::endl;
m_connected = true;
}
virtual void onDisconnect(ConnectionError e) {
std::cout << "Disconnected. Error: " << e << std::endl;
m_connected = false;
}
virtual bool onTLSConnect(const CertInfo& info) {
std::cout << "TLS connected" << std::endl;
return true; // Return true to continue connection process
}
bool isConnected() const {
return m_connected;
}
Client& getClient() {
return m_client;
}
private:
Client& m_client;
bool m_connected;
};
int main() {
const JID jid("test1@localhost");
Client client(jid, "test1");
MyConnectionListener connectionListener(client);
MyMessageHandler messageHandler;
client.registerConnectionListener(&connectionListener);
client.registerMessageHandler(&messageHandler);
client.connect();
// Error 9
return 0;
}
我正在尝试连接到本地 ejabberd 服务器,该服务器是我使用 Docker 应用程序设置的。由于某种原因,我不断收到错误 9。这是我当前正在使用的代码。我尝试了不同的连接方式,在某些时候,当我将
test1@localhost
更改为 test1@test1
时,出现错误 10。我认为它检测到服务器但未连接到它。
我遵守
g++ -o program macheta.cpp -lgloox -lstdc++ -L/usr/local/lib -Wl,-rpath=/usr/local/lib -pthread
并使用
./program
运行应用程序
如何修复错误并连接服务器?
JID jid("test1@localhost"); client = new Client(jid, "test1"); connListener = new ConnListener(); client->registerConnectionListener(connListener); std::string server = "192.168.0.221"; // adresa serverului XMPP int port = 5222; // portul implicit pentru XMPP client->setServer(server); client->setPort(port); client->registerMessageHandler(this); client->connect(true);
需要SetServer和SetPort