feat: implement websocket-stream for journal server

Integrate `websocket-stream` to handle MQTT broker upgrades in the
CLI and add granular connection status tracking to the UI.

- Add `websocket-stream` dependency
- Update CLI to use `websocket-stream` for socket upgrades
- Enhance `journalStream` store with `connectionStatus` and
  `connectionError` states
- Add visual connection status indicators to `App` and `JournalPanel`
This commit is contained in:
2026-07-06 15:38:28 +08:00
parent 862ba9c01b
commit 34d647d611
6 changed files with 116 additions and 12 deletions
+15
View File
@@ -33,6 +33,10 @@ export interface JournalStreamState {
revealedPaths: Set<string>;
/** MQTT connection status */
connected: boolean;
/** Granular connection state for UI indicators */
connectionStatus: "disconnected" | "connecting" | "connected" | "error";
/** Last connection error message, if any */
connectionError: string | null;
/** This client's identity */
myName: string;
/** Broker URL, set after connect */
@@ -84,6 +88,8 @@ const [state, setState] = createStore<JournalStreamState>({
senderSeq: {},
revealedPaths: new Set(),
connected: false,
connectionStatus: "disconnected",
connectionError: null,
myName: persisted.myName,
brokerUrl: persisted.brokerUrl,
});
@@ -189,6 +195,9 @@ export async function connectStream(
): Promise<void> {
const { default: mqtt } = await import("mqtt");
setState("connectionStatus", "connecting");
setState("connectionError", null);
const client = mqtt.connect(brokerUrl, {
clientId: `${state.myName}-${Date.now()}`,
protocol: brokerUrl.startsWith("wss") ? "wss" : "ws",
@@ -200,6 +209,8 @@ export async function connectStream(
client.on("connect", () => {
_mqttConnected = true;
setState("connected", true);
setState("connectionStatus", "connected");
setState("connectionError", null);
setState("brokerUrl", brokerUrl);
// Persist connection info for next time
@@ -251,10 +262,13 @@ export async function connectStream(
client.on("close", () => {
_mqttConnected = false;
setState("connected", false);
setState("connectionStatus", "disconnected");
});
client.on("error", (err) => {
console.error("[stream] mqtt error:", err);
setState("connectionStatus", "error");
setState("connectionError", err.message);
});
}
@@ -416,6 +430,7 @@ export function disconnectStream(): void {
_mqttConnected = false;
}
setState("connected", false);
setState("connectionStatus", "disconnected");
}
// ---------------------------------------------------------------------------