Add polling flag to prevent concurrent requests in long polling

parent 0d39f09f
No preview for this file type
......@@ -484,17 +484,17 @@ static const char *terminal_page_html =
" term.write(data);\n"
" }\n"
" }\n"
" // Schedule next poll\n"
" // Schedule next poll immediately for long polling\n"
" if (pollInterval) {\n"
" pollInterval = setTimeout(pollData, 300);\n"
" pollInterval = setTimeout(pollData, 50);\n"
" }\n"
" }\n"
" })\n"
" .catch(error => {\n"
" console.error('Polling error:', error);\n"
" // Continue polling even on error\n"
" // Continue polling even on error, but with delay\n"
" if (pollInterval) {\n"
" pollInterval = setTimeout(pollData, 300);\n"
" pollInterval = setTimeout(pollData, 1000);\n"
" }\n"
" });\n"
"}\n"
......
......@@ -7,8 +7,8 @@ unsigned char image_jpg[] = {
0x30, 0x00, 0x00, 0xea, 0x60, 0x00, 0x00, 0x3a, 0x98, 0x00, 0x00, 0x17,
0x70, 0x9c, 0xba, 0x51, 0x3c, 0x00, 0x00, 0x00, 0x06, 0x62, 0x4b, 0x47,
0x44, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0xa0, 0xbd, 0xa7, 0x93, 0x00,
0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 0x45, 0x07, 0xe9, 0x09, 0x16, 0x0f,
0x2d, 0x23, 0x0c, 0xec, 0xe6, 0x78, 0x00, 0x00, 0x05, 0xd4, 0x7a, 0x54,
0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 0x45, 0x07, 0xe9, 0x09, 0x16, 0x10,
0x06, 0x26, 0x1d, 0x8c, 0x0b, 0xd3, 0x00, 0x00, 0x05, 0xd4, 0x7a, 0x54,
0x58, 0x74, 0x52, 0x61, 0x77, 0x20, 0x70, 0x72, 0x6f, 0x66, 0x69, 0x6c,
0x65, 0x20, 0x74, 0x79, 0x70, 0x65, 0x20, 0x61, 0x70, 0x70, 0x31, 0x00,
0x00, 0x48, 0x89, 0x85, 0x96, 0x59, 0x92, 0xdc, 0x38, 0x10, 0x43, 0xff,
......
......@@ -119,6 +119,7 @@ let fitAddon = null;
let connected = false;
let requestId = null;
let pollInterval = null;
let polling = false;
console.log('Terminal page loaded, adding event listeners');
document.getElementById('connectBtn').addEventListener('click', connect);
......@@ -426,7 +427,8 @@ function disconnect() {
}
function pollData() {
if (!requestId) return;
if (!requestId || polling) return;
polling = true;
fetch('/terminal/%s/xterm/data?request_id=' + encodeURIComponent(requestId))
.then(response => {
if (response.status !== 200) {
......@@ -462,7 +464,7 @@ function pollData() {
}
// Schedule next poll immediately for long polling
if (pollInterval) {
pollInterval = setTimeout(pollData, 0);
pollInterval = setTimeout(pollData, 50);
}
}
})
......
......@@ -354,9 +354,9 @@ function connect() {
console.log('Launching command:', data.command);
}
term.write('Connected successfully!\r\n');
setTimeout(() => {
pollInterval = setInterval(pollData, 200);
}, 200);
pollInterval = setInterval(pollData, 500);
// Poll immediately to get any buffered output
pollData();
} else {
term.write('Error: ' + (data.error || 'Unknown error') + '\r\n');
disconnect();
......@@ -402,7 +402,7 @@ function disconnect() {
document.getElementById('sshUsername').disabled = false;
if (pollInterval) {
clearInterval(pollInterval);
clearTimeout(pollInterval);
pollInterval = null;
}
......@@ -443,27 +443,34 @@ function pollData() {
console.log('Poll data received:', data);
if (data.ended !== undefined) {
console.log('Session ended, reloading page');
clearInterval(pollInterval);
pollInterval = null;
if (pollInterval) {
clearTimeout(pollInterval);
pollInterval = null;
}
location.reload();
} else if (data) {
console.log('Received data:', data.byteLength || data.length, 'bytes/characters');
// Write data to terminal
if (data.byteLength !== undefined) {
// Binary data
term.write(new Uint8Array(data));
} else {
// Text data
term.write(data);
} else {
if (data) {
console.log('Received data:', data.byteLength || data.length, 'bytes/characters');
// Write data to terminal
if (data.byteLength !== undefined) {
// Binary data
term.write(new Uint8Array(data));
} else {
// Text data
term.write(data);
}
}
// Schedule next poll immediately for long polling
if (pollInterval) {
pollInterval = setTimeout(pollData, 0);
}
}
})
.catch(error => {
console.error('Polling error:', error);
// Stop polling on error
// Continue polling even on error, but with delay
if (pollInterval) {
clearInterval(pollInterval);
pollInterval = null;
pollInterval = setTimeout(pollData, 1000);
}
});
}
......
No preview for this file type
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment