後引用Aardappel
回答我做了以下更改我的代碼來解決此問題。
創建緩衝區文件
$file = 'buffer_content.bin';
$output = $builder->dataBuffer();
$fp = fopen($file, "wb");
fwrite($fp, $output);
fclose($fp);
代碼從文件中獲取緩衝區的內容&響應返回給Ajax調用
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// change these to whatever is appropriate in your code
$my_place = "/path/to/the/file/"; // directory of your file
$my_file = "item.bin"; // your file
//$my_path = $my_place.$my_file;
$my_path = $my_file;
header("Pragma: public");
header("Expires: 0");
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0', false);
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
$browser = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/MSIE 5.5/', $browser) || preg_match('/MSIE 6.0/', $browser))
{
header('Pragma: private');
// the c in control is lowercase, didnt work for me with uppercase
header('Cache-control: private, must-revalidate');
// MUST be a number for IE
header("Content-Length: ".filesize($my_path));
header('Content-Type: application/x-download');
header('Content-Disposition: attachment; filename="'.$my_file.'"');
}
else
{
header("Content-Length: ".(string)(filesize($my_path)));
header('Content-Type: application/x-download');
header('Content-Disposition: attachment; filename="'.$my_file.'"');
}
header('Content-Transfer-Encoding: binary');
if ($file = fopen($my_path, 'rb'))
{
while(!feof($file) and (connection_status()==0))
{
print(fread($file, filesize($my_path)));
flush();
}
fclose($file);
}
?>
代碼在客戶端解析的二進制數據
var xhr = new XMLHttpRequest();
xhr.open('GET', 'getBufferData.php', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
// response is unsigned 8 bit integer
var responseArray = new Uint8Array(this.response);
var buf = new flatbuffers.ByteBuffer(responseArray);
var monster = MyGame.Sample.Monster.getRootAsMonster(buf);
var hp = monster.hp();
var pos = monster.pos();
console.log("hp : "+hp);
console.log("pos : "+pos);
};
xhr.send();
好的,完成了,現在好了。 – mi6crazyheart