1

我想每 2 秒从 WhatsApp 服务器检索一次消息。我正在使用这个 jQuery 函数:

          setInterval(function(){   
                $.post("recevie_message.php",{},function(data){

                       console.log(data);
                       $arr=JSON.parse(data);
                       $.each($arr,function(index,value){
                              console.log(value.body);
                              var templateResponse = Handlebars.compile( $("#message-response-template").html());
                              var contextResponse = {response:value.body};
                   $('.chat-history').append(templateResponse(contextResponse));

                        });
          },2000);

和PHP代码:

<?php
  include_once('checkUserSession.php');
  $w=$task->connectToServer();
 // header ('Content-Type: text/html; charset=UTF-8'); 
  $username= $task->getPhoneNumber();
  $password = $task->getWhatsappPassword();
  $msg = array();
  $i=0;
  function onMessage($mynumber, $from, $id, $type, $time, $name, $body)
{
   $GLOBALS['msg'][$GLOBALS['i']]=array("from"=>$from,"body"=>$body);
   $GLOBALS['task']->saveMessage($body,'f','t',$from);
   $GLOBALS['i']++;
}


      include_once('Chat-API-master/src/events/MyEvents.php');
      $events = new MyEvents($w);
      $w->eventManager()->bind("onGetMessage", "onMessage");

      $w->pollMessage();
      echo json_encode($msg);
      $w->disconnect();


      //$w->disconnect();

    ?>

问题是我loginFaulierException接到了很多电话(不是所有电话)。有没有更好的方式来接收消息,或者换句话说,WhatsApp 原生应用程序是如何工作的?

4

0 回答 0