$recv_length) { // 还有这么多字节要接收 return $total_length - $recv_length; } // 接收完毕 return 0; } // 打包 public static function encode($data) { // 选用json格式化数据 $buffer = json_encode($data); // 包的整体长度为json长度加首部四个字节(首部数据包长度存储占用空间) $total_length = 4 + strlen($buffer); return pack('N', $total_length) . $buffer; } // 解包 public static function decode($buffer) { $buffer_data = unpack('Ntotal_length', $buffer); // 得到这次数据的整体长度(字节) $total_length = $buffer_data['total_length']; // json的数据 $json_string = substr($buffer, 4); return json_decode($json_string, true); } }