[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);
 } 

标签: php

发表评论:

Powered by anycle 湘ICP备15001973号-1