문자열과 같은 방식으로 플로트를 쉽게 보내고받을 수있는 방법이 없다는 것을 이해합니다. 그러나 다음과 같이 내 웹 소켓을 설정하면 :
ws = new WebSocket(address);
ws.binaryType = 'blob';
들어오는 바이트 문자열을 수레로 변환 할 수 있어야합니다. float를 바이트 스트링으로 변환하고 서버 측으로 보내는 것은 쉽습니다.
제가 답을 찾을 수있는 가장 가까운 것은 이것 입니다. 그러나 나는 그것이 e.target.result
정의되지 않았다는 것을 알았습니다 . 을 사용해 보았지만 e.target
컴파일러에서 수정 방법을 알아낼 수없는 유형 오류가 발생했습니다.
같은 질문도있다 이 수레에 UINT 배열을 변환합니다. 근데 이런 게 있으면
ws.onmessage = function(event){
//do something with event.data
}
여기에있는event.data
것과 같은 문자열이 아닐 때 작업하는 방법을 이해해야합니다 .
적응 후 이 답변 하고 이 답변을 , 나는 다음과 같은 해결책을 마련했습니다 :
//open the socket and set the data type to blob
let socket = new WebSocket(address);
socket.binaryType = 'blob';
//we will store 6 positions at a time
let positions = new Float32Array(18);
//helpers
let buffer = new ArrayBuffer(4);
let view = new DataView(buffer);
//say hello
socket.onopen = function(){
socket.send('Hello');
};
//keep track of where we are in the position array
let posIndex = 0;
socket.onmessage = function(msg){
//convert message to Uint8 array
let bitArray = new Uint8Array(msg.data);
for(let i = 0; i < 3; i++){
for(let j = 0; j < 4; j++){
//set the elements of the DataView equal to the bytes
view.setUint8(j, bitArray[4*i + j]);
}
//update the positions
if(posIndex < 5){
positions[3*posIndex + i] = view.getFloat32(0);
posIndex++;
}
else positions[15 + i] = view.getFloat32(0);
}
//this should log the positions as they come in
paragraph.innerHTML = paragraph.innerHTML + ",("
+ positions[posIndex] + ","
+ positions[posIndex + 1] + ","
+ positions[posIndex + 2] + ")";
//the server won't send another position until it hears from the client
socket.send('r');
};
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다