Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

feat: keep the screencast stream alive and more responsive

Boschc2d5b507bb48
diff --git a/dashboard/src/components/screencast/ScreencastTile.tsx b/dashboard/src/components/screencast/ScreencastTile.tsx
index a4aaec9f..80042ae1 100644
--- a/dashboard/src/components/screencast/ScreencastTile.tsx
+++ b/dashboard/src/components/screencast/ScreencastTile.tsx
@@ -19,9 +19,9 @@ export default function ScreencastTile({
tabId,
label,
url,
- quality = 40,
- maxWidth = 800,
- fps = 1,
+ quality = 60,
+ maxWidth = 1280,
+ fps = 10,
showTitle = true,
}: Props) {
const canvasRef = useRef<HTMLCanvasElement>(null);
diff --git a/dashboard/src/components/screencast/useScreencastStream.ts b/dashboard/src/components/screencast/useScreencastStream.ts
index 6c3cc76f..61b54ba6 100644
--- a/dashboard/src/components/screencast/useScreencastStream.ts
+++ b/dashboard/src/components/screencast/useScreencastStream.ts
@@ -23,6 +23,8 @@ export function useScreencastStream({
canvasRef,
}: UseScreencastStreamArgs) {
const socketRef = useRef<WebSocket | null>(null);
+ const reconnectAttemptsRef = useRef(0);
+ const reconnectTimerRef = useRef<number | undefined>(undefined);
const [status, setStatus] = useState<ScreencastStatus>("connecting");
const [fpsDisplay, setFpsDisplay] = useState("—");
const [sizeDisplay, setSizeDisplay] = useState("—");
@@ -37,6 +39,7 @@ export function useScreencastStream({
setStatus("connecting");
setHasFrame(false);
setFallbackUrl(null);
+ reconnectAttemptsRef.current = 0; // fresh tab → fresh reconnect budget
}, [tabId, fps]);
// Clean up static preview URL on unmount or tab change
@@ -114,6 +117,8 @@ export function useScreencastStream({
hasRenderedFrame = true;
window.clearTimeout(fallbackTimer);
+ // A frame arrived: the stream is healthy, so clear the reconnect backoff.
+ reconnectAttemptsRef.current = 0;
setHasFrame(true);
setStatus("streaming");
} catch (err) {
@@ -151,23 +156,37 @@ export function useScreencastStream({
}
};
- socket.onerror = () => {
- if (!disposed) {
+ // A dropped screencast socket is usually transient (the capture pipeline
+ // hiccupped, the tab briefly navigated, a proxy blip). Auto-reconnect with a
+ // short backoff instead of dropping the user into a permanent "connection
+ // lost" overlay. Only after repeated failures do we surface an error and run
+ // the auth-failure path, since a persistently rejected socket may mean the
+ // session is no longer valid.
+ const MAX_RECONNECT_ATTEMPTS = 6;
+ let dropHandled = false;
+ const handleDrop = () => {
+ if (disposed || dropHandled) return;
+ dropHandled = true;
+ if (reconnectAttemptsRef.current >= MAX_RECONNECT_ATTEMPTS) {
setStatus("error");
void api.handleRealtimeAuthFailure();
+ return;
}
+ const attempt = reconnectAttemptsRef.current++;
+ const delay = Math.min(250 * 2 ** attempt, 4000); // 250ms → … → 4s cap
+ setStatus("connecting");
+ reconnectTimerRef.current = window.setTimeout(() => {
+ if (!disposed) setRetryKey((p) => p + 1);
+ }, delay);
};
- socket.onclose = () => {
- if (!disposed) {
- setStatus("error");
- void api.handleRealtimeAuthFailure();
- }
- };
+ socket.onerror = handleDrop;
+ socket.onclose = handleDrop;
return () => {
disposed = true;
window.clearTimeout(fallbackTimer);
+ window.clearTimeout(reconnectTimerRef.current);
socket.close();
socketRef.current = null;
};
diff --git a/dashboard/src/stores/slices/settingsSlice.ts b/dashboard/src/stores/slices/settingsSlice.ts
index 3f4ea32e..c619ccf4 100644
--- a/dashboard/src/stores/slices/settingsSlice.ts
+++ b/dashboard/src/stores/slices/settingsSlice.ts
@@ -3,7 +3,7 @@ import type { Settings } from "../../generated/types";
import type { AppState } from "../useAppStore";
const defaultSettings: Settings = {
- screencast: { fps: 1, quality: 40, maxWidth: 800 },
+ screencast: { fps: 10, quality: 60, maxWidth: 1280 },
stealth: "light",
browser: { blockImages: false, blockMedia: false, noAnimations: false },
monitoring: { memoryMetrics: false, pollInterval: 30 },
diff --git a/internal/assets/screencast_repaint_start.js b/internal/assets/screencast_repaint_start.js
index 1ee74887..90b628e3 100644
--- a/internal/assets/screencast_repaint_start.js
+++ b/internal/assets/screencast_repaint_start.js
@@ -35,6 +35,6 @@
state.host = host;
state.element = el;
state.mount = mount;
- state.timer = setInterval(tick, 250);
+ state.timer = setInterval(tick, 100);
return state.refs;
})()
diff --git a/internal/bridge/screencast.go b/internal/bridge/screencast.go
index 600f0c3c..c7658c5e 100644
--- a/internal/bridge/screencast.go
+++ b/internal/bridge/screencast.go
@@ -30,9 +30,12 @@ func (b *Bridge) StartScreencast(ctx context.Context, opts ScreencastOpts) (*Scr
}
func (b *Bridge) shouldUsePollingScreencast() bool {
- if b.Config != nil && b.Config.Headless {
- return true
- }
+ // Headless is NOT a reason to poll: new-headless ("--headless=new") supports
+ // event-driven Page.startScreencast, and the event-driven path starts a
+ // repaint loop that forces the compositor to keep painting so frames flow even
+ // without a foreground window. Event-driven is far more responsive than polling
+ // CaptureScreenshot per frame, which matters for interactive dashboard control.
+ // Polling remains only for providers whose CDP proxy lacks startScreencast.
if b.Config != nil {
browserID := config.NormalizeBrowser(b.Config.DefaultBrowser)
if browser, ok := browsers.Get(browserID); ok {
@@ -180,17 +183,33 @@ func (b *Bridge) startScreencastPolling(ctx context.Context, opts ScreencastOpts
ticker := time.NewTicker(frameInterval)
defer ticker.Stop()
- var frames int
+ // A single capture failure is usually transient — the target is briefly busy
+ // navigating, painting, or handling a click. Aborting the stream on the first
+ // error drops the dashboard into a "connection lost" overlay mid-interaction.
+ // Skip the bad frame and keep streaming; only give up after many consecutive
+ // failures (or context cancellation), which indicates the target is really gone.
+ const maxConsecutiveErrors = 30
+ var frames, consecutiveErrors int
for {
select {
case <-ticker.C:
t1 := time.Now()
frame, err := capture(ctx)
if err != nil {
- slog.Warn("screencast polling: CaptureScreenshot failed",
- "err", err, "frame", frames, "elapsed", time.Since(t1))
- return
+ if ctx.Err() != nil {
+ return // tab/context gone — stop cleanly
+ }
+ consecutiveErrors++
+ slog.Debug("screencast polling: capture failed, skipping frame",
+ "err", err, "consecutive", consecutiveErrors, "elapsed", time.Since(t1))
+ if consecutiveErrors >= maxConsecutiveErrors {
+ slog.Warn("screencast polling: too many consecutive capture failures, stopping",
+ "consecutive", consecutiveErrors)
+ return
+ }
+ continue
}
+ consecutiveErrors = 0
frames++
if frames <= 3 || frames%30 == 0 {
slog.Debug("screencast polling: frame captured",
diff --git a/internal/handlers/screencast.go b/internal/handlers/screencast.go
index eba5deb6..010a3d85 100644
--- a/internal/handlers/screencast.go
+++ b/internal/handlers/screencast.go
@@ -40,7 +40,11 @@ func (h *Handlers) HandleScreencast(w http.ResponseWriter, r *http.Request) {
quality := queryParamInt(r, "quality", 30)
maxWidth := queryParamInt(r, "maxWidth", 800)
- everyNth := queryParamInt(r, "everyNthFrame", 4)
+ // everyNthFrame=1 sends every compositor frame; the requested fps still throttles
+ // the rate. A higher skip (the old default of 4) caps the live tab well below the
+ // requested fps and makes interactive control feel unresponsive (issue: dashboard
+ // Live tab too laggy to type into).
+ everyNth := queryParamInt(r, "everyNthFrame", 1)
fps := queryParamInt(r, "fps", 1)
if fps > 30 {
fps = 30