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