// simple view

var getxhr;

function sendPac(pac) {
// todo check pac type == arraybuffer
console.log("point1");
var oReq = new XMLHttpRequest();
oReq.open("POST", "/s", true);
oReq.onload = function (oEvent) {console.log("point3");};
console.log("point2");
oReq.send(pac);
}

function processPac(byteArray) {
	let s=">R> ";
    for (var i = 0; i < byteArray.byteLength; i++) {
      // do something with each byte in the array
      s+=byteArray[i];
      s+=" ";
    }    
    document.getElementById("qq").innerHTML+=s;
console.log(s);
}

function getPac() {
console.log("point4");
getxhr = new XMLHttpRequest();
getxhr.open("GET", "/s", true);
getxhr.responseType = "arraybuffer";
getxhr.onreadystatechange = function (oEvent) {
  if(getxhr.readyState === XMLHttpRequest.DONE) {
    var status = getxhr.status;
    if (status === 0 || (status >= 200 && status < 400)) {
  var arrayBuffer = getxhr.response; // Note: not oReq.responseText
  if (arrayBuffer) {
    var byteArray = new Uint8Array(arrayBuffer);
    processPac(byteArray);
    console.log("point6");
  };
  };
  getxhr.open("GET", "/s", true);
setTimeout(() => {  getxhr.send(null); }, 500);  // repeat
};
};
getxhr.send(null);
}


function initialize() {
let bu = new ArrayBuffer(6);
const ui = new Uint8Array(bu);
ui[4]=0xa;
sendPac(bu);
console.log("point5");
getPac();
}


