2016-08-21 2 views
0

웹 응용 프로그램 중 하나에서 플랫 버퍼를 사용하려고합니다. 나는 이미 다음 버퍼 코드를 사용하여 하나의 파일 (buffer_content.txt)에 버퍼 데이터를 저장했다.브라우저에서 flatbuffer 생성 된 데이터를 사용하는 방법?

// ...Code to store to disk or send over a network goes here... 
$file = 'buffer_content.txt'; 
$output = serialize($builder->dataBuffer()); 

$fp = fopen($file, "w"); 
fwrite($fp, $output); 
fclose($fp); 

아약스를 통해 서버에서 버퍼 데이터를 가져올 수 있습니다. 이제 JavaScript에서 버퍼의 원본 데이터를 추출해야합니다. 그러나 나는 그것을 어떻게 할 수 있는지 알 수 없다.

의견이 있으십니까?

+1

좋아요, 끝났습니다. 지금은 괜찮습니다. – mi6crazyheart

답변

0

serialize을 사용하고 싶지 않습니다. 하는 DataBuffer 이미 직렬화 된 데이터, 그것은 여기에서 말하는 것을 체크 아웃 포함 https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html

$ 버피 = $ builder->의 DataBuffer를(); // Of 유형 Google\FlatBuffers\ByteBuffer

//이 ByteBuffer의 데이터는 buf-> getPosition()에서 시작하지 않습니다.

데이터의 끝 buf- 의해 표시된 //

> 용량()이므로 크기

// buf-> 용량()은 - buf->에는 getPosition().

파일을 바이너리 모드로 작성했는지 확인하십시오 ("wb"에서 fopen으로 전달). 또한 텍스트 형식이 아니기 때문에 .txt라고 부르지 마십시오.

JS에서 파일 (다시 텍스트가 아닌 바이너리 모드로)을 읽은 다음 Uint8Array으로 끝나야합니다. 다음 코드를 따르십시오 : https://google.github.io/flatbuffers/flatbuffers_guide_use_javascript.html

+0

답장을 보내 주셔서 감사합니다. 나는이 문제를 해결할 수 있었다. 그러나, 나는이 코드 조각 (원래의 예제 (https://google.github.io/flatbuffers/flatbuffers_guide_use_javascript.html)에서 가져온 것입니다.)에 대한 하나의 이상한 동작이 있습니다. '''var hp = monster.hp(); var pos = monster.pos();'''출력 :'''hp : 100 pos : null'''을 출력하고 있습니다. 반면에 hp = 300을 얻어야합니다. – mi6crazyheart

+0

예'''\ MyGame \ Sample \ Monster :: AddHp ($ builder, 300);'''Ref : https ($ build)에 다음 행에 의해 추가 된 값 (300)이 아닌 스키마에서 기본값을주는 이유는 무엇입니까? : //google.github.io/flatbuffers/flatbuffers_guide_tutorial.html – mi6crazyheart

+0

버퍼가 전송/인코딩/올바르게 디코딩되지 않은 것처럼 들리지만 전체 코드를 보지 않고 말하기는 어렵습니다. – Aardappel

2

Aardappel 대답을 참조한 후이 문제를 해결하기 위해 코드를 변경했습니다.

클라이언트 측에서 바이너리 데이터를 구문 분석에 대한 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); 
} 
?> 

코드로 다시 파일에서 & 응답을 버퍼의 컨텐츠를 가져 오기위한 버퍼 파일을

$file = 'buffer_content.bin'; 
$output = $builder->dataBuffer(); 

$fp = fopen($file, "wb"); 
fwrite($fp, $output); 
fclose($fp); 

코드를 작성

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(); 
관련 문제