如何使用 C++ 将 XMPP 消息节格式化为 GCM - xmpp 握手

How to format XMPP message stanza to GCM using C++ - xmpp handshake

握手已完成并从 GCM xmpp 连接收到 <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>。 现在,我正在格式化并从APP服务器发送同步消息到GCM以发送到移动设备,如下

ostringstream sync;
sync << "<message id=\"1234\">";
sync << "<gcm xmlns=\"google:mobile:data\">";
sync << " { ";
sync << "\"to\":\"eA_hcHITvwo:APA91bEB83Gci\",";
sync << " \"message_id\":\"1234\",";
sync << " \"time_to_live\":" << 10;
sync << " } ";
sync << "</gcm></message>";
string msg = sync.str();
sslSock_->write(msg.c_str(), msg.length());
std::string syncResp = readFromGcm(); // ack/nack

不幸的是,SSL 套接字正在关闭且未收到任何确认。知道吗,我在这里做错了什么?

我尝试发送的实际消息如下所示

Sending msg=<message id="150827111504042682"><gcm xmlns="google:mobile:data"> { "to":"eA_hcHITvwo:APA91bEB83Gci", "message_id":"150827111504042682", "time_to_live":10 } </gcm></message>

我是否需要在收到来自 GCM 的成功消息后发送任何节之前发送任何确认? 或者我需要在花括号和名称值对之后添加换行符吗?

消息流 Sent1: <stream:stream to='gcm.googleapis.com' version='1.0' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'> Received1: <stream:stream from="gcm.googleapis.com" id="EBA37684C852364D" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client"> Received2: <stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>X-OAUTH2</mechanism><mechanism>X-GOOGLE-TOKEN</mechanism><mechanism>PLAIN</mechanism></mechanisms></stream:features> Sent2: <auth mechanism='PLAIN' xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>ADM2NTU4MjFZVV0F1LVJV</auth> Received3: <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/> Sent3: <message id=""><gcm xmlns="google:mobile:data">{"to":"eA_hcHITvwo:APA91bEB83Gci","message_id":"150827161659237590","data":{"title":"Alert: mydata","text":"mydata refresh"},"time_to_live":10,"delay_while_idle":true,"delivery_receipt_requested":true}</gcm></message> Received4: None. Socket closed

我刚刚注意到设备令牌 (eA_hcHITvwo:APA91bEB83Gci) 中有冒号?我是否需要将它转换成某种东西,以便 google xmpp 处理器不会与 json 冒号 ("to":"deviceToken") 混淆?

您在末尾缺少一个逗号:

sync << "'to':'eA_hcHITvwo:APA91bEB83Gci'";,

应该是这样才有效:

sync << "'to':'eA_hcHITvwo:APA91bEB83Gci',";

也许这会解决您的问题。

终于从https://developers.google.com/cloud-print/docs/rawxmpp那里得到了答案 和 http://grokbase.com/p/gg/android-gcm/13a2zrnn34/gcm-ccs-xmpp-3rd-party-server-implementation

xmpp握手的另一个例子 https://tech.yandex.com/disk/doc/dg/concepts/xmpp_xmpp-connection-docpage/

感谢提问的Lee Chua。不幸的是,不清楚是谁回答的,但非常感谢最终回答的人。 不确定为什么协议没有记录在正确的位置。

C (authenticate): <stream:stream to='gcm.googleapis.com' version='1.0' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'> <auth mechanism='PLAIN' xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>base64Enc([=10=][userName][=10=][password])</auth> S: <stream:stream from="gcm.googleapis.com" id="352584B87B657F7F" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client"> <stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>X-OAUTH2</mechanism><mechanism>X-GOOGLE-TOKEN</mechanism><mechanism>PLAIN</mechanism></mechanisms></stream:features> <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/> C (pre Binding): <stream:stream to='gcm.googleapis.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'> S: <stream:stream from="gcm.googleapis.com" id="7C0C770AED97331C" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client"> <stream:features><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features> C: (bind): <iq type='set' id='bind-1' to='gcm.googleapis.com'> <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'> <resource>core</resource> </bind> </iq> S: <iq from="gcm.googleapis.com" id="bind-1" type="result"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>[senderID]@gcm.googleapis.com/coreAB8CB2E7</jid></bind></iq> C: (session request): <iq to='gcm.googleapis.com' type='set' id='sess_1'> <session xmlns='urn:ietf:params:xml:ns:xmpp-session'/> </iq> S: single byte message and then S: <iq from="gcm.googleapis.com" type="result" id="sess_1"/> C: first stanza sent successfully