Update: changes in 3DInput mit WSS
This commit is contained in:
@@ -2,33 +2,71 @@ var startTime = Date.now() ;
|
||||
console.log("Meine Document-Location: " + document.location);
|
||||
var lastPingRequest;
|
||||
|
||||
//var socket = new WebSocket(String(document.location).replace("https://","wss://").replace("10010","2095").replace("index.html","") + "echo");
|
||||
var socket = new WebSocket("wss://thinkcentre.local:2095/echo");
|
||||
socket.onopen = () => console.log('Connected') || setInterval(() => { lastPingRequest = Date.now();
|
||||
socket.send("Ping");
|
||||
}, 5000);
|
||||
socket.onclose = (event) => console.log((event.wasClean) ? 'Disconnected' : 'Connection break: ' + (event.reason || event.code));
|
||||
// Use explicit '/echo' path and a small wrapper to guard sends from other modules
|
||||
var socketUrl = (location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host + '/echo';
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
if(event.data == "Ping"){
|
||||
console.log("Ping: " + (Date.now() - lastPingRequest).toString() + " ms");
|
||||
// socket wrapper keeps the same global identifier but guards sends when underlying ws is not open
|
||||
var socket = {
|
||||
_ws: null,
|
||||
send: function(msg) {
|
||||
if (this._ws && this._ws.readyState === WebSocket.OPEN) {
|
||||
this._ws.send(msg);
|
||||
} else {
|
||||
console.warn('Socket not open, dropping message:', msg);
|
||||
}
|
||||
}
|
||||
else 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];
|
||||
console.log("File Content: " + content);
|
||||
console.log(document.getElementById("GCodeWindow"))
|
||||
|
||||
console.log(document.querySelectorAll("textarea#GCodeWindow.editor-look")[0]);
|
||||
};
|
||||
|
||||
document.querySelectorAll("textarea#GCodeWindow.editor-look")[0].value = content;
|
||||
}
|
||||
else{
|
||||
console.log('DATA SinceStartup: ' + (Date.now() - startTime).toString() +': ', event.data);
|
||||
}
|
||||
let pingIntervalId = null;
|
||||
let reconnectDelay = 1000;
|
||||
|
||||
function connect() {
|
||||
const ws = new WebSocket(socketUrl);
|
||||
socket._ws = ws;
|
||||
|
||||
ws.onopen = () => {
|
||||
console.log('Connected');
|
||||
reconnectDelay = 1000;
|
||||
if (pingIntervalId) clearInterval(pingIntervalId);
|
||||
pingIntervalId = setInterval(() => {
|
||||
if (socket._ws && socket._ws.readyState === WebSocket.OPEN) {
|
||||
lastPingRequest = Date.now();
|
||||
socket.send('Ping');
|
||||
}
|
||||
}, 5000);
|
||||
};
|
||||
|
||||
ws.onclose = (event) => {
|
||||
console.log((event.wasClean) ? 'Disconnected' : 'Connection break: ' + (event.reason || event.code));
|
||||
if (pingIntervalId) { clearInterval(pingIntervalId); pingIntervalId = null; }
|
||||
setTimeout(() => {
|
||||
console.log('Reconnecting...');
|
||||
connect();
|
||||
}, reconnectDelay);
|
||||
reconnectDelay = Math.min(30000, reconnectDelay * 2);
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
if(event.data == 'Ping'){
|
||||
console.log('Ping: ' + (Date.now() - lastPingRequest).toString() + ' ms');
|
||||
}
|
||||
else 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];
|
||||
console.log('File Content: ' + content);
|
||||
const el = document.querySelector('textarea#GCodeWindow.editor-look');
|
||||
if (el) el.value = content;
|
||||
}
|
||||
else{
|
||||
console.log('DATA SinceStartup: ' + (Date.now() - startTime).toString() +': ', event.data);
|
||||
}
|
||||
};
|
||||
|
||||
ws.onerror = (err) => console.error('WebSocket error:', err || err.message);
|
||||
}
|
||||
socket.onerror = (err) => console.error(err.message);
|
||||
|
||||
connect();
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user