我正在使用 libjingle 编写一些代码,但第一步遇到了麻烦:登录到 XMPP 服务器。我的代码基于goog 的示例代码和 pcp 示例代码。这段代码让我有点困惑,因为它似乎只运行一个线程,所以我意识到这是一个非常基本的问题。
无论如何,这是我的代码的主要内容:
talk_base::PhysicalSocketServer ss;
talk_base::AutoThread main_thread(&ss);
buzz::Jid jid( xmppUsername + "@" + xmppHost );
if (!jid.IsValid() || jid.node() == "")
throw "Invalid JID. JIDs should be in the form user@domain" ;
buzz::TlsOptions tls = buzz::TLS_ENABLED;
buzz::XmppClientSettings xcs;
xcs.set_user(jid.node());
xcs.set_host(jid.domain());
xcs.set_resource("pcp");
xcs.set_pass(talk_base::CryptString(pass));
xcs.set_allow_plain(true);
xcs.set_server(talk_base::SocketAddress(xmppHost.c_str(), 5222));
xcs.set_use_tls(tls);
// Log in.
CustomXmppPump pump;
pump.client()->SignalLogInput.connect(&debug_log_, &DebugLog::Input);
pump.client()->SignalLogOutput.connect(&debug_log_, &DebugLog::Output);
pump.DoLogin(xcs, new XmppSocket(tls), 0);
// Wait until login succeeds.
std::vector<uint32> ids;
ids.push_back(MSG_LOGIN_COMPLETE);
ids.push_back(MSG_LOGIN_FAILED);
if (MSG_LOGIN_FAILED == Loop(ids))
throw "Failed to connect";
...
// Runs the current thread until a message with the given ID is seen.
uint32 Loop(const std::vector<uint32>& ids) {
talk_base::Message msg;
while (talk_base::Thread::Current()->Get(&msg)) {
cout << "received message: " << msg.message_id << endl;
if (msg.phandler == NULL) {
if (std::find(ids.begin(), ids.end(), msg.message_id) != ids.end())
return msg.message_id;
std::cout << "orphaned message: " << msg.message_id << endl;
continue;
}
cout << "1: " << msg.message_id << " : " << msg.ts_sensitive << endl;
talk_base::Thread::Current()->Dispatch(&msg);
cout << "2: " << msg.message_id << endl;
}
return 0;
}
运行时,它输出:
连接... 收到的消息:0 1:0:0 [004:722] 解析 PhysicalSocket::Connect 中的地址 2:0 收到的消息:0 1:0:0 2:0
而且只是挂起,很明显它卡在 Get(&msg) 调用上。
我应该注意我的服务器使用 DNS SRV 记录并且可以与其他客户端正常工作——也许我只需要自己解析 SRV?
谢谢你的帮助!