Strophe Ping 插件没有响应

Strophe Ping Plugin Not responding

我正在尝试添加 Strophe ping 插件以 ping 另一个用户并用 pong 响应,然后我知道该用户已连接。

但我不确定如何让它工作。

我在文档中阅读了一个示例,但它对我不起作用。

我只想收到某种形式的回电,说对方已连接。

   conn.addHandler(pingHandler, "urn:xmpp:ping", "iq", "get");


 function pingHandler(ping) {
 var pingId = ping.getAttribute("1");
 var from = ping.getAttribute("from");
 var to = ping.getAttribute("to");
 var pong = $iq({type: "result", "to": from, id: pingId, "from": to});
 conn.send(pong);
  return true;
}    

我也试过下面的方法,下面的方法会发出Ping并调用成功,但是在pingHandler中没有响应。好像发了,没听to/handled.

  conn.ping.addPingHandler( pingHandler );  

 function Onping(){


    var jid3="test4@macbook-pro.local"


     console.log("ping starts");
conn.ping.ping( jid3, success, error, timeout );


     function success(){
     console.log("ping sent");

     }
       function error(){
     console.log("ping Not sent");

     }
       function timeout(){
     console.log("ping Timeout");

     }





 }


            function pingHandler(ping) {
                  console.log("pong back");
     var pingId = ping.getAttribute("id");
     var from =  ping.getAttribute("from");
     var to = ping.getAttribute("to");
     var pong = $iq({type: "result", "to": from, id: pingId, "from": to});
     conn.send(pong);
      return true;
    } 

编辑

SENT: <body rid='4132220829' xmlns='http://jabber.org/protocol/httpbind' sid='a7411f98'><iq type='get' to='test4sdfsdfs@macbook-pro.local' id='1:ping' xmlns='jabber:client'><ping xmlns='urn:xmpp:ping'/></iq></body>

RECV: <body xmlns='http://jabber.org/protocol/httpbind' ack='4132220829'/>

RECV: <body xmlns='http://jabber.org/protocol/httpbind' ack='4132220829'><iq xmlns='jabber:client' type='result' id='1:ping' from='test4sdfsdfs@macbook-pro.local' to='test3@macbook-pro.local/a7411f98'/></body>

您需要在连接时附加一个发送乒乓球的 ping listener/handler。您的听众会响应发送给您的任何 ping。您的 ping 发送器设置了一个处理程序来处理 pong。

var onConn = function(){
    conn.ping.addPingHandler(onPing);
};

var onPing = function (iq) {
    conn.ping.pong(iq);
    return true;
};

var onPong = function(iq){
    //do whatever with response
    return false;
};

var sendPing = function(jid){
    conn.ping.ping(
        jid,
        function (iq) {onPong(iq);},
        function () { console.log('Failed!'); }, 
        3000);
};

sendPing('foo@jabberservername.com');