Implement long polling for terminal data to reduce server load

parent 436bcac1
No preview for this file type
...@@ -486,7 +486,7 @@ static const char *terminal_page_html = ...@@ -486,7 +486,7 @@ static const char *terminal_page_html =
" }\n" " }\n"
" // Schedule next poll\n" " // Schedule next poll\n"
" if (pollInterval) {\n" " if (pollInterval) {\n"
" pollInterval = setTimeout(pollData, 100);\n" " pollInterval = setTimeout(pollData, 300);\n"
" }\n" " }\n"
" }\n" " }\n"
" })\n" " })\n"
...@@ -494,7 +494,7 @@ static const char *terminal_page_html = ...@@ -494,7 +494,7 @@ static const char *terminal_page_html =
" console.error('Polling error:', error);\n" " console.error('Polling error:', error);\n"
" // Continue polling even on error\n" " // Continue polling even on error\n"
" if (pollInterval) {\n" " if (pollInterval) {\n"
" pollInterval = setTimeout(pollData, 100);\n" " pollInterval = setTimeout(pollData, 300);\n"
" }\n" " }\n"
" });\n" " });\n"
"}\n" "}\n"
......
...@@ -8,7 +8,7 @@ unsigned char image_jpg[] = { ...@@ -8,7 +8,7 @@ unsigned char image_jpg[] = {
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, 0x0f,
0x29, 0x29, 0x88, 0x55, 0xca, 0x62, 0x00, 0x00, 0x05, 0xd4, 0x7a, 0x54, 0x2d, 0x23, 0x0c, 0xec, 0xe6, 0x78, 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,
......
...@@ -460,9 +460,9 @@ function pollData() { ...@@ -460,9 +460,9 @@ function pollData() {
term.write(data); term.write(data);
} }
} }
// Schedule next poll // Schedule next poll immediately for long polling
if (pollInterval) { if (pollInterval) {
pollInterval = setTimeout(pollData, 300); pollInterval = setTimeout(pollData, 0);
} }
} }
}) })
...@@ -470,7 +470,7 @@ function pollData() { ...@@ -470,7 +470,7 @@ function pollData() {
console.error('Polling error:', error); console.error('Polling error:', error);
// Continue polling even on error // Continue polling even on error
if (pollInterval) { if (pollInterval) {
pollInterval = setTimeout(pollData, 300); pollInterval = setTimeout(pollData, 0);
} }
}); });
} }
......
...@@ -1332,6 +1332,14 @@ static void handle_request(int client_fd, const http_request_t *req) { ...@@ -1332,6 +1332,14 @@ static void handle_request(int client_fd, const http_request_t *req) {
session_ended = true; session_ended = true;
} else { } else {
output = terminal_get_output(active_terminals[i], &output_len); output = terminal_get_output(active_terminals[i], &output_len);
// Long polling: if no data, wait up to 5 seconds for data
if (output_len == 0) {
for (int wait = 0; wait < 5; wait++) {
sleep(1);
output = terminal_get_output(active_terminals[i], &output_len);
if (output_len > 0) break;
}
}
} }
break; break;
} }
......
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