/* 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);
}