苗火 Nicholas
[PHP]subscribe in mqtt by php
2015-9-22 萧


 /* subscribe: subscribes to topics */

 function subscribe($topics, $qos = 1){

  $i = 0;

  $buffer = "";

  

  $id = $this->msgid++;

  $buffer .= chr($id >> 8);  $i++;

  $buffer .= chr($id % 256);  $i++;

  foreach($topics as $key => $topic){

   $len = strlen($key);

   $buffer .= chr($len >> 8);  $i++;

   $buffer .= chr($len % 256);  $i++;

   $buffer .= $key;

   $i += strlen($key);



   $buffer .= chr($topic["qos"]);  $i++;

   $this->topics[$key] = $topic;

  }





  $head = " ";

  $cmd = 0x80;

  if($qos) $cmd += $qos << 1;



  $head{0} = chr($cmd); 

  $head .= chr($i); 



  fwrite($this->socket, $head, strlen($head));

  fwrite($this->socket, $buffer, $i);

 } 

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容