const protocolS = location.protocol === "https:" ? "wss://" : "ws://";
const robotURL = protocolS + location.host + "/ws/robot";
console.log("socketDriver try to connect to "+ robotURL);

const socketDriver = new WebSocket(robotURL);



socketDriver.onopen = () => { console.log("socketDriver WebSocket connected"); };

socketDriver.onmessage = (event) => {
    if(event.data.toString().includes("position")){
        console.log("Position: " + event.data);
    }
    else if(event.data.toString().includes("XYZ__FShow__XYZ")){
        const content = event.data.toString().split("XYZ__FShow__XYZ")[1];
        document.querySelectorAll("textarea#GCodeWindow.editor-look")[0].value = content;
    }
    else{
        console.log('DATA  SinceStartup: ' +  (Date.now() - startTime).toString() +':  ', event.data);
    }
}

socketDriver.onclose = () => {console.log("socketDriver WebSocket is closing");};
socketDriver.onerror = (err) => { console.error("socketDriver socket error:", err); };